debian-mirror-gitlab/doc/development/database_review.md

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

277 lines
18 KiB
Markdown
Raw Normal View History

2021-01-03 14:25:43 +05:30
---
2022-07-23 23:45:48 +05:30
stage: Data Stores
2021-01-03 14:25:43 +05:30
group: Database
2021-02-22 17:27:13 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2021-01-03 14:25:43 +05:30
---
2019-10-12 21:52:04 +05:30
# Database Review Guidelines
2022-07-23 23:45:48 +05:30
This page is specific to database reviews. Refer to our
2019-10-12 21:52:04 +05:30
[code review guide](code_review.md) for broader advice and best
practices for code review in general.
## General process
A database review is required for:
- Changes that touch the database schema or perform data migrations,
including files in:
- `db/`
- `lib/gitlab/background_migration/`
2020-04-08 14:13:33 +05:30
- Changes to the database tooling. For example:
2019-10-12 21:52:04 +05:30
- migration or ActiveRecord helpers in `lib/gitlab/database/`
- load balancing
- Changes that produce SQL queries that are beyond the obvious. It is
generally up to the author of a merge request to decide whether or
not complex queries are being introduced and if they require a
database review.
2022-07-23 23:45:48 +05:30
- Changes in Service Data metrics that use `count`, `distinct_count`, `estimate_batch_distinct_count` and `sum`.
2020-07-28 23:09:34 +05:30
These metrics could have complex queries over large tables.
2021-03-08 18:12:59 +05:30
See the [Product Intelligence Guide](https://about.gitlab.com/handbook/product/product-intelligence-guide/)
2020-07-28 23:09:34 +05:30
for implementation details.
2019-10-12 21:52:04 +05:30
2021-04-29 21:17:54 +05:30
A database reviewer is expected to look out for overly complex
2019-10-12 21:52:04 +05:30
queries in the change and review those closer. If the author does not
2021-04-29 21:17:54 +05:30
point out specific queries for review and there are no overly
2019-10-12 21:52:04 +05:30
complex queries, it is enough to concentrate on reviewing the
migration only.
2021-02-22 17:27:13 +05:30
### Required
2021-11-18 22:05:49 +05:30
You must provide the following artifacts when you request a ~database review.
2022-07-23 23:45:48 +05:30
If your merge request description does not include these items, the review is reassigned back to the author.
2021-02-22 17:27:13 +05:30
2021-11-18 22:05:49 +05:30
#### Migrations
2021-02-22 17:27:13 +05:30
If new migrations are introduced, in the MR **you are required to provide**:
2021-09-30 23:02:18 +05:30
- The output of both migrating (`db:migrate`) and rolling back (`db:rollback`) for all migrations.
2021-02-22 17:27:13 +05:30
2022-07-23 23:45:48 +05:30
We have automated tooling for
2021-11-18 22:05:49 +05:30
[GitLab](https://gitlab.com/gitlab-org/gitlab) (provided by the
2022-04-04 11:22:00 +05:30
[`db:check-migrations`](database/dbcheck-migrations-job.md) pipeline job) that provides this output for migrations on
2021-11-18 22:05:49 +05:30
~database merge requests. You do not need to provide this information manually
if the bot can do it for you. The bot also checks that migrations are correctly
reversible.
#### Queries
2021-02-22 17:27:13 +05:30
If new queries have been introduced or existing queries have been updated, **you are required to provide**:
- [Query plans](#query-plans) for each raw SQL query included in the merge request along with the link to the query plan following each raw SQL snippet.
- [Raw SQL](#raw-sql) for all changed or added queries (as translated from ActiveRecord queries).
- In case of updating an existing query, the raw SQL of both the old and the new version of the query should be provided together with their query plans.
Refer to [Preparation when adding or modifying queries](#preparation-when-adding-or-modifying-queries) for how to provide this information.
2019-10-12 21:52:04 +05:30
### Roles and process
2022-04-04 11:22:00 +05:30
A merge request **author**'s role is to:
2019-10-12 21:52:04 +05:30
- Decide whether a database review is needed.
- If database review is needed, add the ~database label.
2019-12-04 20:38:33 +05:30
- [Prepare the merge request for a database review](#how-to-prepare-the-merge-request-for-a-database-review).
2021-02-22 17:27:13 +05:30
- Provide the [required](#required) artifacts prior to submitting the MR.
2019-10-12 21:52:04 +05:30
A database **reviewer**'s role is to:
2021-02-22 17:27:13 +05:30
- Ensure the [required](#required) artifacts are provided and in the proper format. If they are not, reassign the merge request back to the author.
2019-10-12 21:52:04 +05:30
- Perform a first-pass review on the MR and suggest improvements to the author.
- Once satisfied, relabel the MR with ~"database::reviewed", approve it, and
2021-04-29 21:17:54 +05:30
request a review from the database **maintainer** suggested by Reviewer
Roulette. Remove yourself as a reviewer once this has been done.
2019-10-12 21:52:04 +05:30
A database **maintainer**'s role is to:
- Perform the final database review on the MR.
- Discuss further improvements or other relevant changes with the
database reviewer and the MR author.
- Finally approve the MR and relabel the MR with ~"database::approved"
- Merge the MR if no other approvals are pending or pass it on to
2022-07-23 23:45:48 +05:30
other maintainers as required (frontend, backend, documentation).
2021-04-29 21:17:54 +05:30
- If not merging, remove yourself as a reviewer.
2019-10-12 21:52:04 +05:30
### Distributing review workload
Review workload is distributed using [reviewer roulette](code_review.md#reviewer-roulette)
2020-03-13 15:44:24 +05:30
([example](https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/25181#note_147551725)).
2021-04-29 21:17:54 +05:30
The MR author should request a review from the suggested database
2022-07-23 23:45:48 +05:30
**reviewer**. When they sign off, they hand over to
2019-10-12 21:52:04 +05:30
the suggested database **maintainer**.
If reviewer roulette didn't suggest a database reviewer & maintainer,
make sure you have applied the ~database label and rerun the
`danger-review` CI job, or pick someone from the
[`@gl-database` team](https://gitlab.com/groups/gl-database/-/group_members).
2019-12-04 20:38:33 +05:30
### How to prepare the merge request for a database review
2019-10-12 21:52:04 +05:30
2022-07-23 23:45:48 +05:30
To make reviewing easier and therefore faster, take
2020-03-13 15:44:24 +05:30
the following preparations into account.
2019-10-12 21:52:04 +05:30
2020-03-13 15:44:24 +05:30
#### Preparation when adding migrations
2020-10-24 23:57:45 +05:30
- Ensure `db/structure.sql` is updated as [documented](migration_style_guide.md#schema-changes), and additionally ensure that the relevant version files under
`db/schema_migrations` were added or removed.
2020-03-13 15:44:24 +05:30
- Make migrations reversible by using the `change` method or include a `down` method when using `up`.
- Include either a rollback procedure or describe how to rollback changes.
2021-09-30 23:02:18 +05:30
- Add the output of both migrating (`db:migrate`) and rolling back (`db:rollback`) for all migrations into the MR description.
2020-05-24 23:13:21 +05:30
- Ensure the down method reverts the changes in `db/structure.sql`.
- Update the migration output whenever you modify the migrations during the review process.
2020-04-08 14:13:33 +05:30
- Add tests for the migration in `spec/migrations` if necessary. See [Testing Rails migrations at GitLab](testing_guide/testing_migrations_guide.md) for more details.
2021-11-11 11:23:49 +05:30
- When [high-traffic](https://gitlab.com/gitlab-org/gitlab/-/blob/master/rubocop/rubocop-migrations.yml#L3) tables are involved in the migration, use the [`enable_lock_retries`](migration_style_guide.md#retry-mechanism-when-acquiring-database-locks) method to enable lock-retries. Review the relevant [examples in our documentation](migration_style_guide.md#usage-with-transactional-migrations) for use cases and solutions.
2020-04-22 19:07:51 +05:30
- Ensure RuboCop checks are not disabled unless there's a valid reason to.
2020-06-23 00:09:42 +05:30
- When adding an index to a [large table](https://gitlab.com/gitlab-org/gitlab/-/blob/master/rubocop/rubocop-migrations.yml#L3),
test its execution using `CREATE INDEX CONCURRENTLY` in the `#database-lab` Slack channel and add the execution time to the MR description:
- Execution time largely varies between `#database-lab` and GitLab.com, but an elevated execution time from `#database-lab`
2022-07-23 23:45:48 +05:30
can give a hint that the execution on GitLab.com is also considerably high.
2022-06-21 17:19:12 +05:30
- If the execution from `#database-lab` is longer than `1h`, the index should be moved to a [post-migration](database/post_deployment_migrations.md).
2020-06-23 00:09:42 +05:30
Keep in mind that in this case you may need to split the migration and the application changes in separate releases to ensure the index
2022-07-23 23:45:48 +05:30
is in place when the code that needs it is deployed.
2021-10-27 15:23:28 +05:30
- Manually trigger the [database testing](database/database_migration_pipeline.md) job (`db:gitlabcom-database-testing`) in the `test` stage.
2021-09-30 23:02:18 +05:30
- This job runs migrations in a production-like environment (similar to `#database_lab`) and posts to the MR its findings (queries, runtime, size change).
- Review migration runtimes and any warnings.
2020-03-13 15:44:24 +05:30
2022-03-02 08:16:31 +05:30
#### Preparation when adding data migrations
Data migrations are inherently risky. Additional actions are required to reduce the possibility
of error that would result in corruption or loss of production data.
Include in the MR description:
2022-07-23 23:45:48 +05:30
- If the migration itself is not reversible, details of how data changes could be reverted in the event of an incident. For example, in the case of a migration that deletes records (an operation that most of the times is not automatically reversible), how _could_ the deleted records be recovered.
2022-03-02 08:16:31 +05:30
- If the migration deletes data, apply the label `~data-deletion`.
- Concise descriptions of possible user experience impact of an error; for example, "Issues would unexpectedly go missing from Epics".
2022-07-23 23:45:48 +05:30
- Relevant data from the [query plans](#query-plans) that indicate the query works as expected; such as the approximate number of records that are modified or deleted.
2022-03-02 08:16:31 +05:30
2020-03-13 15:44:24 +05:30
#### Preparation when adding or modifying queries
2021-02-22 17:27:13 +05:30
##### Raw SQL
2020-03-13 15:44:24 +05:30
- Write the raw SQL in the MR description. Preferably formatted
2020-06-23 00:09:42 +05:30
nicely with [pgFormatter](https://sqlformat.darold.net) or
2021-01-29 00:20:46 +05:30
[paste.depesz.com](https://paste.depesz.com) and using regular quotes
2022-07-23 23:45:48 +05:30
<!-- vale gitlab.NonStandardQuotes = NO -->
2021-10-27 15:23:28 +05:30
(for example, `"projects"."id"`) and avoiding smart quotes (for example, `“projects”.“id”`).
2022-07-23 23:45:48 +05:30
<!-- vale gitlab.NonStandardQuotes = YES -->
2021-02-22 17:27:13 +05:30
- In case of queries generated dynamically by using parameters, there should be one raw SQL query for each variation.
For example, a finder for issues that may take as a parameter an optional filter on projects,
2022-07-23 23:45:48 +05:30
should include both the version of the query over issues and the one that joins issues
2021-02-22 17:27:13 +05:30
and projects and applies the filter.
There are finders or other methods that can generate a very large amount of permutations.
There is no need to exhaustively add all the possible generated queries, just the one with
all the parameters included and one for each type of queries generated.
For example, if joins or a group by clause are optional, the versions without the group by clause
and with less joins should be also included, while keeping the appropriate filters for the remaining tables.
2022-07-23 23:45:48 +05:30
- If a query is always used with a limit and an offset, those should always be
2021-02-22 17:27:13 +05:30
included with the maximum allowed limit used and a non 0 offset.
##### Query Plans
- The query plan for each raw SQL query included in the merge request along with the link to the query plan following each raw SQL snippet.
2021-03-11 19:13:27 +05:30
- Provide a public link to the plan from either:
- [postgres.ai](https://postgres.ai/): Follow the link in `#database-lab` and generate a shareable, public link
2022-07-23 23:45:48 +05:30
by clicking **Share** in the upper right corner.
2021-09-30 23:02:18 +05:30
- [explain.depesz.com](https://explain.depesz.com) or [explain.dalibo.com](https://explain.dalibo.com): Paste both the plan and the query used in the form.
2020-03-13 15:44:24 +05:30
- When providing query plans, make sure it hits enough data:
- You can use a GitLab production replica to test your queries on a large scale,
2021-03-11 19:13:27 +05:30
through the `#database-lab` Slack channel or through [ChatOps](understanding_explain_plans.md#chatops).
2020-03-13 15:44:24 +05:30
- Usually, the `gitlab-org` namespace (`namespace_id = 9970`) and the
`gitlab-org/gitlab-foss` (`project_id = 13083`) or the `gitlab-org/gitlab` (`project_id = 278964`)
projects provide enough data to serve as a good example.
2021-02-22 17:27:13 +05:30
- That means that no query plan should return 0 records or less records than the provided limit (if a limit is included). If a query is used in batching, a proper example batch with adequate included results should be identified and provided.
2021-12-11 22:18:48 +05:30
- If your queries belong to a new feature in GitLab.com and thus they don't return data in production:
- You may analyze the query and to provide the plan from a local environment.
- `#database-lab` and [postgres.ai](https://postgres.ai/) both allow updates to data (`exec UPDATE issues SET ...`) and creation of new tables and columns (`exec ALTER TABLE issues ADD COLUMN ...`).
2021-02-22 17:27:13 +05:30
- More information on how to find the number of actual returned records in [Understanding EXPLAIN plans](understanding_explain_plans.md)
- For query changes, it is best to provide both the SQL queries along with the
plan _before_ and _after_ the change. This helps spot differences quickly.
2020-03-13 15:44:24 +05:30
- Include data that shows the performance improvement, preferably in
the form of a benchmark.
#### Preparation when adding foreign keys to existing tables
- Include a migration to remove orphaned rows in the source table **before** adding the foreign key.
- Remove any instances of `dependent: ...` that may no longer be necessary.
#### Preparation when adding tables
- Order columns based on the [Ordering Table Columns](ordering_table_columns.md) guidelines.
- Add foreign keys to any columns pointing to data in other tables, including [an index](migration_style_guide.md#adding-foreign-key-constraints).
- Add indexes for fields that are used in statements such as `WHERE`, `ORDER BY`, `GROUP BY`, and `JOIN`s.
2022-05-07 20:08:51 +05:30
- New tables and columns are not necessarily risky, but over time some access patterns are inherently
2022-07-23 23:45:48 +05:30
difficult to scale. To identify these risky patterns in advance, we must document expectations for
2022-05-07 20:08:51 +05:30
access and size. Include in the MR description answers to these questions:
- What is the anticipated growth for the new table over the next 3 months, 6 months, 1 year? What assumptions are these based on?
- How many reads and writes per hour would you expect this table to have in 3 months, 6 months, 1 year? Under what circumstances are rows updated? What assumptions are these based on?
2022-07-23 23:45:48 +05:30
- Based on the anticipated data volume and access patterns, does the new table pose an availability risk to GitLab.com or self-managed instances? Does the proposed design scale to support the needs of GitLab.com and self-managed customers?
2020-03-13 15:44:24 +05:30
2020-04-08 14:13:33 +05:30
#### Preparation when removing columns, tables, indexes, or other structures
2020-03-13 15:44:24 +05:30
2022-06-21 17:19:12 +05:30
- Follow the [guidelines on dropping columns](database/avoiding_downtime_in_migrations.md#dropping-columns).
2020-04-08 14:13:33 +05:30
- Generally it's best practice (but not a hard rule) to remove indexes and foreign keys in a post-deployment migration.
2020-03-13 15:44:24 +05:30
- Exceptions include removing indexes and foreign keys for small tables.
2020-04-08 14:13:33 +05:30
- If you're adding a composite index, another index might become redundant, so remove that in the same migration.
For example adding `index(column_A, column_B, column_C)` makes the indexes `index(column_A, column_B)` and `index(column_A)` redundant.
2019-10-12 21:52:04 +05:30
### How to review for database
- Check migrations
- Review relational modeling and design choices
2022-06-21 17:19:12 +05:30
- Consider [access patterns and data layout](database/layout_and_access_patterns.md) if new tables or columns are added.
2019-10-12 21:52:04 +05:30
- Review migrations follow [database migration style guide](migration_style_guide.md),
for example
- [Check ordering of columns](ordering_table_columns.md)
- [Check indexes are present for foreign keys](migration_style_guide.md#adding-foreign-key-constraints)
- Ensure that migrations execute in a transaction or only contain
concurrent index/foreign key helpers (with transactions disabled)
2020-06-23 00:09:42 +05:30
- If an index to a large table is added and its execution time was elevated (more than 1h) on `#database-lab`:
- Ensure it was added in a post-migration.
- Maintainer: After the merge request is merged, notify Release Managers about it on `#f_upcoming_release` Slack channel.
2020-04-22 19:07:51 +05:30
- Check consistency with `db/structure.sql` and that migrations are [reversible](migration_style_guide.md#reversibility)
2020-10-24 23:57:45 +05:30
- Check that the relevant version files under `db/schema_migrations` were added or removed.
2021-02-22 17:27:13 +05:30
- Check queries timing (If any): In a single transaction, cumulative query time executed in a migration
2022-07-23 23:45:48 +05:30
needs to fit comfortably in `15s` - preferably much less than that - on GitLab.com.
2022-06-21 17:19:12 +05:30
- For column removals, make sure the column has been [ignored in a previous release](database/avoiding_downtime_in_migrations.md#dropping-columns)
- Check [background migrations](database/background_migrations.md):
2020-01-01 13:55:28 +05:30
- Establish a time estimate for execution on GitLab.com. For historical purposes,
it's highly recommended to include this estimation on the merge request description.
- If a single `update` is below than `1s` the query can be placed
2019-10-12 21:52:04 +05:30
directly in a regular migration (inside `db/migrate`).
2020-01-01 13:55:28 +05:30
- Background migrations are normally used, but not limited to:
- Migrating data in larger tables.
- Making numerous SQL queries per record in a dataset.
2019-10-12 21:52:04 +05:30
- Review queries (for example, make sure batch sizes are fine)
- Because execution time can be longer than for a regular migration,
it's suggested to treat background migrations as post migrations:
place them in `db/post_migrate` instead of `db/migrate`. Keep in mind
that post migrations are executed post-deployment in production.
2022-06-21 17:19:12 +05:30
- If a migration [has tracking enabled](database/background_migrations.md#background-jobs-tracking),
ensure `mark_all_as_succeeded` is called even if no work is done.
2021-10-27 15:23:28 +05:30
- Check [timing guidelines for migrations](migration_style_guide.md#how-long-a-migration-should-take)
2019-12-21 20:55:43 +05:30
- Check migrations are reversible and implement a `#down` method
2022-05-07 20:08:51 +05:30
- Check new table migrations:
- Are the stated access patterns and volume reasonable? Do the assumptions they're based on seem sound? Do these patterns pose risks to stability?
- Are the columns [ordered to conserve space](ordering_table_columns.md)?
- Are there foreign keys for references to other tables?
2019-12-21 20:55:43 +05:30
- Check data migrations:
- Establish a time estimate for execution on GitLab.com.
2020-04-08 14:13:33 +05:30
- Depending on timing, data migrations can be placed on regular, post-deploy, or background migrations.
2019-12-21 20:55:43 +05:30
- Data migrations should be reversible too or come with a description of how to reverse, when possible.
This applies to all types of migrations (regular, post-deploy, background).
2019-10-12 21:52:04 +05:30
- Query performance
2021-04-29 21:17:54 +05:30
- Check for any overly complex queries and queries the author specifically
2019-10-12 21:52:04 +05:30
points out for review (if any)
2022-07-23 23:45:48 +05:30
- If not present, ask the author to provide SQL queries and query plans
2021-03-11 19:13:27 +05:30
(for example, by using [ChatOps](understanding_explain_plans.md#chatops) or direct
2019-10-12 21:52:04 +05:30
database access)
- For given queries, review parameters regarding data distribution
- [Check query plans](understanding_explain_plans.md) and suggest improvements
to queries (changing the query, schema or adding indexes and similar)
2021-02-22 17:27:13 +05:30
- General guideline is for queries to come in below [100ms execution time](query_performance.md#timing-guidelines-for-queries)
2021-03-11 19:13:27 +05:30
- Avoid N+1 problems and minimize the [query count](merge_request_performance_guidelines.md#query-counts).