Skip to content

Conversation

@TylerWitt
Copy link
Contributor

Validation on FK constraints in postgres can cause large tables to stay locked for a relatively long time because it has to check all rows for valid data, while also preventing new entries.

Postgres implemented a way to not validate the constraint immediately so the FK can be immediately inserted, improving concurrency, with the intention that a VALIDATE CONSTRAINT query will be following.

The validation query uses a less expensive lock, because it only needs to check rows that existed before the constraint. This allows safer migrations, and also allows for users to clean up known violations while still enforcing the constraint for new entries.

This commit implements the ability to skip validation, but it does not implement the validation query.

See https://www.postgresql.org/docs/12/sql-altertable.html for more details

Validation on FK constraints in postgres can cause large tables to stay locked for a relatively long time because it has to check all rows for valid data, while also preventing new entries.

Postgres implemented a way to not validate the constraint immediately so the FK can be immediately inserted, improving concurrency, with the intention that a `VALIDATE CONSTRAINT` query will be following.

The validation query uses a less expensive lock, because it only needs to check rows that existed before the constraint. This allows safer migrations, and also allows for users to clean up known violations while still enforcing the constraint for new entries.

This commit implements the ability to skip validation, but it does not implement the validation query.
Copy link
Member

@josevalim josevalim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two minor comments added for consistency. You will have to update the error messages after committing them though. :)

@TylerWitt
Copy link
Contributor Author

Done! Did you want me to squash it down too?

@josevalim josevalim merged commit cd7f585 into elixir-ecto:master Jul 6, 2020
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

zachdaniel pushed a commit to zachdaniel/ecto_sql that referenced this pull request Sep 19, 2020
Validation on FK constraints in postgres can cause large tables to stay locked for a relatively long time because it has to check all rows for valid data, while also preventing new entries.

Postgres implemented a way to not validate the constraint immediately so the FK can be immediately inserted, improving concurrency, with the intention that a `VALIDATE CONSTRAINT` query will be following.

The validation query uses a less expensive lock, because it only needs to check rows that existed before the constraint. This allows safer migrations, and also allows for users to clean up known violations while still enforcing the constraint for new entries.

This commit implements the ability to skip validation, but it does not implement the validation query.
jbernardo95 added a commit to jbernardo95/ecto_sql that referenced this pull request Sep 23, 2020
Validation of constraints in PostgreSQL can cause large tables to stay
locked for a long time (preventing writes, because it has to check all
rows in order to consider the constraint as valid.

PostgreSQL has a way to get around this by first creating constraints as
not valid. And then running a query to validate the constraint. The
validation query does not lock the table, i.e. it can be run while
allowing writes in parallel.

This commit implements the ability to skip validation of constraints. It
does not implement the validation query.

This was inspired by elixir-ecto#244.

https://www.postgresql.org/docs/current/sql-altertable.html
josevalim pushed a commit that referenced this pull request Sep 24, 2020
…te (#271)

Validation of constraints in PostgreSQL can cause large tables to stay
locked for a long time (preventing writes, because it has to check all
rows in order to consider the constraint as valid.

PostgreSQL has a way to get around this by first creating constraints as
not valid. And then running a query to validate the constraint. The
validation query does not lock the table, i.e. it can be run while
allowing writes in parallel.

This commit implements the ability to skip validation of constraints. It
does not implement the validation query.

This was inspired by #244.

https://www.postgresql.org/docs/current/sql-altertable.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants