Skip to content

Add support for nulls_not_distinct option in create_constraint and create_index operations #740

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

Closed
wants to merge 13 commits into from

Conversation

kvch
Copy link
Contributor

@kvch kvch commented Mar 24, 2025

Add support for nulls_not_distinct option in create_constraint and create_index operations for unique constraints and indexes.

NULLS NOT DISTINCT was introduced in PG 15, so I added some conditionals to check if we can retrieve the column from pg_index. Without this function, we get an exception because indnullsnotdisctict does not exist in PG 14.

Closes #730

@github-actions github-actions bot temporarily deployed to Docs Preview March 24, 2025 13:23 Inactive
@kvch kvch marked this pull request as draft March 24, 2025 13:27
@kvch kvch changed the title Add support for nulls_not_distinct option in create_constraint operations Add support for nulls_not_distinct option in create_constraint operation for unique constraints Mar 24, 2025
@kvch kvch force-pushed the kvch/add-nulls-not-distinct branch from e1bad97 to 6ff0f9e Compare March 24, 2025 13:34
@github-actions github-actions bot temporarily deployed to Docs Preview March 24, 2025 13:34 Inactive
@kvch kvch force-pushed the kvch/add-nulls-not-distinct branch from 6ff0f9e to 7e2b9b5 Compare March 24, 2025 13:43
@github-actions github-actions bot temporarily deployed to Docs Preview March 24, 2025 13:43 Inactive
@kvch kvch force-pushed the kvch/add-nulls-not-distinct branch from 7e2b9b5 to 8ab51cf Compare March 24, 2025 13:47
@github-actions github-actions bot temporarily deployed to Docs Preview March 24, 2025 13:47 Inactive
@kvch kvch force-pushed the kvch/add-nulls-not-distinct branch from 8ab51cf to d0ec26c Compare March 24, 2025 13:51
@github-actions github-actions bot temporarily deployed to Docs Preview March 24, 2025 13:51 Inactive
@kvch kvch force-pushed the kvch/add-nulls-not-distinct branch from d0ec26c to 4d894cb Compare March 24, 2025 14:21
@github-actions github-actions bot temporarily deployed to Docs Preview March 24, 2025 14:21 Inactive
@kvch kvch changed the title Add support for nulls_not_distinct option in create_constraint operation for unique constraints Add support for nulls_not_distinct option in create_constraint and create_index operations Mar 24, 2025
@kvch kvch force-pushed the kvch/add-nulls-not-distinct branch 2 times, most recently from f6c1033 to 7575377 Compare March 25, 2025 19:11
@kvch kvch marked this pull request as ready for review March 25, 2025 19:14
@kvch kvch force-pushed the kvch/add-nulls-not-distinct branch from 18f1010 to 0e9be3e Compare March 25, 2025 19:15
@kvch kvch requested a review from andrew-farries March 25, 2025 19:17
@andrew-farries andrew-farries requested a review from Copilot March 26, 2025 16:05
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for the new nulls_not_distinct option used to indicate that nulls are considered equal in unique constraints and indexes when supported by PostgreSQL 15. Key changes include:

  • Adding a new boolean field for nulls_not_distinct in constraint and index migration types.
  • Updating SQL generation functions to optionally include the "NULLS NOT DISTINCT" clause.
  • Enforcing that nulls_not_distinct is only applicable for unique constraints and updating tests and duplicate operations accordingly.

Reviewed Changes

Copilot reviewed 9 out of 15 changed files in this pull request and generated no comments.

Show a summary per file
File Description
pkg/migrations/types.go Adds new bool field (NullsNotDistinct) to migration types
pkg/schema/schema.go Adds new field in Index and UniqueConstraint structures with JSON mapping for nulls_not_distinct
pkg/migrations/op_create_constraint.go Passes the new nullsNotDistinct parameter and validates constraint type
pkg/migrations/op_create_index.go Condition to append "NULLS NOT DISTINCT" in index creation SQL
pkg/migrations/index.go Updates function signatures and SQL generation for unique indexes
pkg/migrations/duplicate.go Updates unique index duplication to include nullsNotDistinct flag
pkg/migrations/duplicate_test.go Adjusts test calls to include the new parameter
pkg/migrations/op_set_unique.go Updates call to createUniqueIndexConcurrently with false for nullsNotDistinct
pkg/migrations/op_add_column.go Updates call to createUniqueIndexConcurrently with false for nullsNotDistinct
Files not reviewed (6)
  • docs/operations/create_constraint.mdx: Language not supported
  • docs/operations/create_index.mdx: Language not supported
  • examples/44_add_table_unique_constraint.json: Language not supported
  • internal/jsonschema/testdata/create-constraint-3-invalid-check-nulls.txtar: Language not supported
  • pkg/state/init.sql: Language not supported
  • schema.json: Language not supported
Comments suppressed due to low confidence (1)

pkg/schema/schema.go:118

  • [nitpick] Consider using snake_case ("nulls_not_distinct") for the JSON tag to align with the migration types' naming convention.
NullsNotDistinct bool `json:"nullsNotDistinct"`

Copy link
Collaborator

@andrew-farries andrew-farries left a comment

Choose a reason for hiding this comment

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

👍 Left some comments.

In addition, we should also add tests in op_create_constraint_test.go and op_create_index_test.go to check that NULLS NOT DISTINCT is applied correctly when set.

@github-actions github-actions bot temporarily deployed to Docs Preview March 27, 2025 10:07 Inactive
@github-actions github-actions bot temporarily deployed to Docs Preview March 27, 2025 14:32 Inactive
@github-actions github-actions bot temporarily deployed to Docs Preview March 27, 2025 14:39 Inactive
@github-actions github-actions bot temporarily deployed to Docs Preview March 27, 2025 14:41 Inactive
@github-actions github-actions bot temporarily deployed to Docs Preview March 27, 2025 15:48 Inactive
@github-actions github-actions bot temporarily deployed to Docs Preview March 27, 2025 16:13 Inactive
@kvch kvch force-pushed the kvch/add-nulls-not-distinct branch from 5764cd5 to 5de6ea7 Compare March 27, 2025 16:17
@github-actions github-actions bot temporarily deployed to Docs Preview March 27, 2025 16:17 Inactive
@kvch kvch force-pushed the kvch/add-nulls-not-distinct branch from 6d8d642 to 5de6ea7 Compare March 27, 2025 16:28
@github-actions github-actions bot temporarily deployed to Docs Preview March 27, 2025 16:28 Inactive
@kvch kvch force-pushed the kvch/add-nulls-not-distinct branch from 5de6ea7 to e09788a Compare March 27, 2025 16:33
@github-actions github-actions bot temporarily deployed to Docs Preview March 27, 2025 16:34 Inactive
@kvch kvch requested a review from andrew-farries March 27, 2025 16:35
Copy link
Collaborator

@andrew-farries andrew-farries left a comment

Choose a reason for hiding this comment

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

Looks good.

We still need to add tests in op_create_constraint_test.go and op_create_index_test.go to check that nulls_not_distinct: true is applied correctly when set. Currently we don't have anything to check that indexes/constraints created with nulls_not_distinct: true actually results in a NULLS NOT DISTINCT index/constraint.

@@ -491,6 +494,43 @@ func TestReadSchema(t *testing.T) {
},
},
},
{
name: "unique, nulls not distinct",
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we also need a test for reading a unique constraint with created with NULLS NOT DISTINCT.

@github-actions github-actions bot temporarily deployed to Docs Preview March 28, 2025 10:44 Inactive
@github-actions github-actions bot temporarily deployed to Docs Preview March 28, 2025 14:16 Inactive
@kvch kvch force-pushed the kvch/add-nulls-not-distinct branch from 524be8e to 401d13a Compare March 28, 2025 15:30
@github-actions github-actions bot temporarily deployed to Docs Preview March 28, 2025 15:30 Inactive
@kvch kvch force-pushed the kvch/add-nulls-not-distinct branch from 401d13a to 832935c Compare April 4, 2025 08:25
@github-actions github-actions bot temporarily deployed to Docs Preview April 4, 2025 08:26 Inactive
@github-actions github-actions bot temporarily deployed to Docs Preview April 4, 2025 08:29 Inactive
@kvch kvch marked this pull request as draft April 8, 2025 12:28
@kvch
Copy link
Contributor Author

kvch commented Apr 8, 2025

I am converting this to draft for now because we need to rework backfilling process significantly to get this working.

@kvch
Copy link
Contributor Author

kvch commented Jul 14, 2025

Closing because it is outdated.

@kvch kvch closed this Jul 14, 2025
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.

Support NULLS NOT DISTINCT
2 participants