2020-10-05 15:08:56 +00:00
---
stage: Enablement
group: Database
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/#designated-technical-writers
---
2019-07-19 17:33:48 +00:00
# Database Review Guidelines
This page is specific to database reviews. Please refer to our
[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-03-02 09:07:59 +00:00
- Changes to the database tooling. For example:
2019-07-19 17:33:48 +00:00
- 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.
2020-07-17 00:09:37 +00:00
- Changes in usage data metrics that use `count` and `distinct_count` .
These metrics could have complex queries over large tables.
2020-10-08 18:08:32 +00:00
See the [Product Analytics Guide ](product_analytics/usage_ping.md#implementing-usage-ping )
2020-07-17 00:09:37 +00:00
for implementation details.
2019-07-19 17:33:48 +00:00
A database reviewer is expected to look out for obviously complex
queries in the change and review those closer. If the author does not
point out specific queries for review and there are no obviously
complex queries, it is enough to concentrate on reviewing the
migration only.
It is preferable to review queries in SQL form and generally accepted
to ask the author to translate any ActiveRecord queries in SQL form
for review.
### Roles and process
2019-12-26 21:07:49 +00:00
A Merge Request **author** 's role is to:
2019-07-19 17:33:48 +00:00
- Decide whether a database review is needed.
- If database review is needed, add the ~database label.
2019-09-04 03:09:05 +00:00
- [Prepare the merge request for a database review ](#how-to-prepare-the-merge-request-for-a-database-review ).
2019-07-19 17:33:48 +00:00
A database **reviewer** 's role is to:
- 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
reassign MR to the database **maintainer** suggested by Reviewer
Roulette.
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
other maintainers as required (frontend, backend, docs).
### Distributing review workload
Review workload is distributed using [reviewer roulette ](code_review.md#reviewer-roulette )
2020-02-06 15:09:11 +00:00
([example](https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/25181#note_147551725)).
2019-07-19 17:33:48 +00:00
The MR author should then co-assign the suggested database
**reviewer**. When they give their sign-off, they will hand over to
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-09-04 03:09:05 +00:00
### How to prepare the merge request for a database review
2019-07-25 16:30:45 +00:00
2019-12-26 21:07:49 +00:00
In order to make reviewing easier and therefore faster, please take
the following preparations into account.
2019-07-25 16:30:45 +00:00
2019-12-26 21:07:49 +00:00
#### Preparation when adding migrations
2020-07-23 15:09:28 +00:00
- 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.
2019-12-26 21:07:49 +00:00
- 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.
2020-04-29 15:09:58 +00:00
- Add the output of both migrating and rolling back for all migrations into the MR description.
- 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-03-12 06:09:35 +00:00
- 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.
2020-06-15 21:08:35 +00:00
- When [high-traffic ](https://gitlab.com/gitlab-org/gitlab/-/blob/master/rubocop/rubocop-migrations.yml#L3 ) tables are involved in the migration, use the [`with_lock_retries` ](migration_style_guide.md#retry-mechanism-when-acquiring-database-locks ) helper method. Review the relevant [examples in our documentation ](migration_style_guide.md#examples ) for use cases and solutions.
2020-04-03 09:09:31 +00:00
- Ensure RuboCop checks are not disabled unless there's a valid reason to.
2020-06-15 21:08:35 +00:00
- When adding an index to a [large table ](https://gitlab.com/gitlab-org/gitlab/-/blob/master/rubocop/rubocop-migrations.yml#L3 ),
2020-06-10 15:08:07 +00:00
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`
can give a hint that the execution on GitLab.com will also be considerably high.
- If the execution from `#database-lab` is longer than `1h` , the index should be moved to a [post-migration ](post_deployment_migrations.md ).
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
will be in place when the code that needs it will be deployed.
2019-12-26 21:07:49 +00:00
#### Preparation when adding or modifying queries
- Write the raw SQL in the MR description. Preferably formatted
2020-06-10 03:08:17 +00:00
nicely with [pgFormatter ](https://sqlformat.darold.net ) or
2019-12-26 21:07:49 +00:00
[paste.depesz.com ](https://paste.depesz.com ).
- Include the output of `EXPLAIN (ANALYZE, BUFFERS)` of the relevant
queries in the description. If the output is too long, wrap it in
`<details>` blocks, paste it in a GitLab Snippet, or provide the
link to the plan at: [explain.depesz.com ](https://explain.depesz.com ).
- 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,
through the `#database-lab` Slack channel or through [chatops ](understanding_explain_plans.md#chatops ).
- 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.
- For query changes, it is best to provide the SQL query along with a
plan _before_ and _after_ the change. This helps to spot differences
quickly.
- 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.
2020-03-02 09:07:59 +00:00
#### Preparation when removing columns, tables, indexes, or other structures
2019-12-26 21:07:49 +00:00
- Follow the [guidelines on dropping columns ](what_requires_downtime.md#dropping-columns ).
2020-03-02 09:07:59 +00:00
- Generally it's best practice (but not a hard rule) to remove indexes and foreign keys in a post-deployment migration.
2019-12-26 21:07:49 +00:00
- Exceptions include removing indexes and foreign keys for small tables.
2020-03-16 21:09:21 +00:00
- 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-07-25 16:30:45 +00:00
2019-07-19 17:33:48 +00:00
### How to review for database
- Check migrations
- Review relational modeling and design choices
- 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-10 15:08:07 +00:00
- 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-03-22 15:09:49 +00:00
- Check consistency with `db/structure.sql` and that migrations are [reversible ](migration_style_guide.md#reversibility )
2020-07-23 15:09:28 +00:00
- Check that the relevant version files under `db/schema_migrations` were added or removed.
2019-08-09 06:47:03 +00:00
- Check queries timing (If any): Queries executed in a migration
Migrations guide: use atomic steps, when possible
Currently, the DB migrations guide says that "you must" use non-blocking
operations (such as CREATE INDEX CONCURRENTLY), always.
But this does not make sense in cases of empty tables and leads to
splitting the work to multiple non-atomic
(with disable_ddl_transaction!) DB migrations.
To follow KISS principle, to have fewer DB migrations steps,
to have them atomic when it's possible and simplify deployment
and troubleshooting, the following exceptions were added
to the doc:
- index creation,
- index dropping,
- defining an FK,
- adding a column with DEFAULT,
2019-08-27 10:22:12 +00:00
need to fit comfortably within `15s` - preferably much less than that - on GitLab.com.
2019-12-01 06:06:11 +00:00
- For column removals, make sure the column has been [ignored in a previous release ](what_requires_downtime.md#dropping-columns )
2019-08-09 06:47:03 +00:00
- Check [background migrations ](background_migrations.md ):
2019-12-13 15:08:02 +00:00
- 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-08-09 06:47:03 +00:00
directly in a regular migration (inside `db/migrate` ).
2019-12-13 15:08:02 +00:00
- Background migrations are normally used, but not limited to:
- Migrating data in larger tables.
- Making numerous SQL queries per record in a dataset.
2019-07-19 17:33:48 +00:00
- Review queries (for example, make sure batch sizes are fine)
2019-08-09 06:47:03 +00:00
- 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.
- Check [timing guidelines for migrations ](#timing-guidelines-for-migrations )
2019-09-27 09:06:26 +00:00
- Check migrations are reversible and implement a `#down` method
2019-10-04 18:05:55 +00:00
- Check data migrations:
- Establish a time estimate for execution on GitLab.com.
2020-03-02 09:07:59 +00:00
- Depending on timing, data migrations can be placed on regular, post-deploy, or background migrations.
2019-10-04 18:05:55 +00:00
- 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-07-19 17:33:48 +00:00
- Query performance
- Check for any obviously complex queries and queries the author specifically
points out for review (if any)
- If not present yet, ask the author to provide SQL queries and query plans
2020-03-02 09:07:59 +00:00
(for example, by using [chatops ](understanding_explain_plans.md#chatops ) or direct
2019-07-19 17:33:48 +00:00
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)
- General guideline is for queries to come in below 100ms execution time
2019-12-26 21:07:49 +00:00
- Avoid N+1 problems and minimalize the [query count ](merge_request_performance_guidelines.md#query-counts ).
2019-08-09 06:47:03 +00:00
2019-10-04 12:06:14 +00:00
### Timing guidelines for migrations
2019-08-09 06:47:03 +00:00
In general, migrations for a single deploy shouldn't take longer than
1 hour for GitLab.com. The following guidelines are not hard rules, they were
estimated to keep migration timing to a minimum.
2020-07-16 06:09:33 +00:00
NOTE: **Note:**
Keep in mind that all runtimes should be measured against GitLab.com.
2019-08-09 06:47:03 +00:00
| Migration Type | Execution Time Recommended | Notes |
|----|----|---|
2020-03-13 09:09:23 +00:00
| Regular migrations on `db/migrate` | `3 minutes` | A valid exception are index creation as this can take a long time. |
2019-08-09 06:47:03 +00:00
| Post migrations on `db/post_migrate` | `10 minutes` | |
2020-03-02 12:07:57 +00:00
| Background migrations | --- | Since these are suitable for larger tables, it's not possible to set a precise timing guideline, however, any single query must stay below `1 second` execution time with cold caches. |