Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for deferrable exclude constraints in PostgreSQL. #47655

Merged
merged 1 commit into from
Mar 14, 2023

Conversation

alpaca-tc
Copy link
Contributor

@alpaca-tc alpaca-tc commented Mar 13, 2023

Motivation / Background

By default, exclude constraints in PostgreSQL are checked after each row. This works for most use cases, but becomes a major limitation when replacing records with overlapping ranges by using multiple statements.

exclusion_constraint :users, "daterange(valid_from, valid_to) WITH &&", deferrable: :immediate

Passing deferrable: :immediate checks constraint after each statement, but allows manually deferring the check using SET CONSTRAINTS ALL DEFERRED within a transaction. This will cause the excludes to be checked after the transaction.

It's also possible to change the default behavior from an immediate check (after the statement), to a deferred check (after the transaction):

exclusion_constraint :users, "daterange(valid_from, valid_to) WITH &&", deferrable: :deferred

Detail

This is follow up to #40224

Additional information

It is with reference to this comment that the deferrable option accepts only :immediate and :deferred.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

Copy link
Member

@yahonda yahonda left a comment

Choose a reason for hiding this comment

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

Thanks for the follow-up pull request.

By default, exclude constraints in PostgreSQL are checked after each statement.
This works for most use cases, but becomes a major limitation when replacing
records with overlapping ranges by using multiple statements.

```ruby
exclusion_constraint :users, "daterange(valid_from, valid_to) WITH &&", deferrable: :immediate
```

Passing `deferrable: :immediate` checks constraint after each statement,
but allows manually deferring the check using `SET CONSTRAINTS ALL DEFERRED`
within a transaction. This will cause the excludes to be checked after the transaction.

It's also possible to change the default behavior from an immediate check
(after the statement), to a deferred check (after the transaction):

```ruby
exclusion_constraint :users, "daterange(valid_from, valid_to) WITH &&", deferrable: :deferred
```

*Hiroyuki Ishii*
@alpaca-tc alpaca-tc force-pushed the deferrable_exclusion_constraint branch from df4d576 to cbc7b59 Compare March 14, 2023 14:30
@alpaca-tc
Copy link
Contributor Author

@yahonda I have fixed the PR based on your review comments. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants