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

Support NULLS NOT DISTINCT in Postgres 15+ #48608

Merged
merged 7 commits into from
Aug 3, 2023

Conversation

grjones
Copy link
Contributor

@grjones grjones commented Jun 29, 2023

Motivation / Background

PostgreSQL 15 introduced NULLS NOT DISTINCT in order to support unique indexes where you want to treat NULLs as equal. Otherwise, and by default, unique indexes allow any rows with null column values.

This Pull Request has been created because creating unique indexes where rows can have only one null value is helpful, and it is a good idea to keep up with new PostgreSQL features.

Detail

This Pull Request changes the PostgreSQL connection adapter, schema detection, and schema generation. Prior work was done in #46659 but was incomplete--a DB created from schema.rb would not have NULLS NOT DISTINCT set. Additionally, #44803 added INCLUDE to the index schema regex, but this must come before NULLS [NOT] DISTINCT per the Postgres docs.

Additional information

Check out the PostreSQL docs here, in particular to confirm the correct ordering of SQL parts. My intent was to put it immediately after INCLUDE in regex detection and ruby argument order.

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.

@grjones grjones force-pushed the postgres-index-nulls-not-distinct branch from 38ef9e8 to 575a2e0 Compare June 29, 2023 05:56
@grjones
Copy link
Contributor Author

grjones commented Jun 29, 2023

Buildkite failed for unrelated Trilogy tests but this looks to be a known issue: #48605

Co-authored-by: Nikita Vasilevsky <nvasilevskii@gmail.com>
@grjones grjones force-pushed the postgres-index-nulls-not-distinct branch from 97505ec to 8aa9b1b Compare June 29, 2023 16:14
@grjones grjones requested a review from nvasilevski June 29, 2023 16:22
@grjones
Copy link
Contributor Author

grjones commented Jul 18, 2023

@nvasilevski Let me know if there is anything more I can do. Many thanks.

@multiplegeorges
Copy link

@grjones Thanks for taking this PR on! It's exactly what I need. Any updates available?

@rafaelfranca rafaelfranca merged commit fbaba19 into rails:main Aug 3, 2023
davinlagerroos pushed a commit to umn-asr/oracle-enhanced that referenced this pull request Feb 21, 2024
rails/rails#48608 adds an additional valid
index option. Mirror that change here to allow the activerecord
test schema to build
kamipo added a commit to kamipo/rails that referenced this pull request Oct 26, 2024
rails#48608 implemented NULLS NOT DISTINCT for UNIQUE INDEX, but it also
works for UNIQUE CONSTRAINT.
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.

4 participants