Skip to content

sql/schemachanger: fix ALTER PRIMARY KEY idempotency for hash-sharded indexes#164557

Merged
trunk-io[bot] merged 1 commit intocockroachdb:masterfrom
rafiss:fix-hash-idempotent
Mar 2, 2026
Merged

sql/schemachanger: fix ALTER PRIMARY KEY idempotency for hash-sharded indexes#164557
trunk-io[bot] merged 1 commit intocockroachdb:masterfrom
rafiss:fix-hash-idempotent

Conversation

@rafiss
Copy link
Collaborator

@rafiss rafiss commented Feb 27, 2026

Previously, running ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (j) USING HASH twice on the same table would fail instead of being recognized as a no-op. The idempotency check in
isNewPrimaryKeySameAsOldPrimaryKey compared the old index's key columns (which include the shard column, e.g.
[crdb_internal_j_shard_16, j]) against the user-specified columns ([j]). The length mismatch caused the function to return false, proceeding with a redundant ALTER that then errored.

Fix this by moving the sharded-vs-not-sharded check earlier and, when both old and new PKs are hash-sharded, filtering out the shard column from the old index's key columns before comparison.

Resolves: #130191
Resolves: #113587

Release note (bug fix): Fixed a bug where running ALTER TABLE ... ALTER PRIMARY KEY USING COLUMNS (...) USING HASH a second time on a table that already had the same hash-sharded primary key would fail instead of being treated as a no-op.

@trunk-io
Copy link
Contributor

trunk-io bot commented Feb 27, 2026

😎 Merged successfully - details.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@rafiss rafiss marked this pull request as ready for review March 2, 2026 16:34
@rafiss rafiss requested a review from a team as a code owner March 2, 2026 16:34
@rafiss rafiss requested review from fqazi and shghasemi March 2, 2026 16:34
Copy link
Collaborator

@fqazi fqazi left a comment

Choose a reason for hiding this comment

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

One minor issue with this

@fqazi reviewed all commit messages and made 3 comments.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on rafiss and shghasemi).


pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_alter_primary_key.go line 408 at r1 (raw file):

	// shard column, but the old index's key columns do.
	if oldPrimaryIndexElem.Sharding != nil {
		shardColID := getColumnIDFromColumnName(

I think this still needs to confirm the bucket count? Since that could have changed?


pkg/sql/logictest/testdata/logic_test/alter_primary_key line 1440 at r1 (raw file):

# Repeating the same ALTER PRIMARY KEY with USING HASH should be a no-op.
statement ok
ALTER TABLE t1 ALTER PRIMARY KEY USING COLUMNS (j) USING HASH;

Should we have a builder test to confirm no elements get generated or execute explain?

… indexes

Previously, ALTER TABLE ... ALTER PRIMARY KEY USING COLUMNS (col) USING
HASH would not be treated as idempotent even when the primary key was
already hash-sharded with the same columns. This happened because the
comparison logic in `isNewPrimaryKeySameAsOldPrimaryKey` did not account
for hash-sharded indexes: the old primary key's key columns include the
shard column, but the user-specified columns do not.

This commit fixes the comparison to filter out the shard column from the
old index's key columns before comparing, and also checks that the
bucket count matches. If the primary key is already hash-sharded with
the same columns and bucket count, the ALTER is correctly treated as a
no-op.

Fixes: cockroachdb#130191

Release note (bug fix): ALTER TABLE ... ALTER PRIMARY KEY USING COLUMNS
(col) USING HASH is now correctly treated as a no-op when the table
already has a matching hash-sharded primary key, instead of attempting
an unnecessary schema change.

Co-Authored-By: roachdev-claude <roachdev-claude-bot@cockroachlabs.com>
@rafiss rafiss force-pushed the fix-hash-idempotent branch from 06bccf3 to 840f29c Compare March 2, 2026 19:36
@rafiss
Copy link
Collaborator Author

rafiss commented Mar 2, 2026

Addressed both review comments:

Bucket count check (line 408): The bucket count comparison was already present further down in the same function (lines 440-449 at r1). It evaluates the new shard bucket count and compares it against the old one, returning false if they differ. I've added a builder test case that explicitly demonstrates changing the bucket count is not a no-op.

Builder test (line 1440): Added both:

  1. A builder test in scbuild/testdata/alter_table_alter_primary_key confirming zero elements are generated for the no-op case (and that changing bucket count does generate elements).
  2. An EXPLAIN (DDL) query in the logic test confirming only UndoAllInTxnImmediateMutationOpSideEffects is produced (no real schema change operations).

Copy link
Collaborator

@fqazi fqazi left a comment

Choose a reason for hiding this comment

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

:lgtm:

@fqazi reviewed 3 files and all commit messages, made 1 comment, and resolved 1 discussion.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on rafiss and shghasemi).

@rafiss
Copy link
Collaborator Author

rafiss commented Mar 2, 2026

/trunk merge

TFTR!

@trunk-io trunk-io bot merged commit 8209d95 into cockroachdb:master Mar 2, 2026
26 checks passed
@rafiss rafiss deleted the fix-hash-idempotent branch March 3, 2026 06:14
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.

schemachanger: creating hash-sharded PK not idempotent schemachanger: ALTER PK with hashing fails if new and old PK is identical

3 participants