Skip to content

[YSQL] Fixes unique index consistency bug when yb_enable_inplace_index_update is on#32126

Open
austenLacy wants to merge 2 commits into
yugabyte:masterfrom
Shopify:al-fix-uniq-idx-consistency-bug-with-yb_enable_inplace_index_update
Open

[YSQL] Fixes unique index consistency bug when yb_enable_inplace_index_update is on#32126
austenLacy wants to merge 2 commits into
yugabyte:masterfrom
Shopify:al-fix-uniq-idx-consistency-bug-with-yb_enable_inplace_index_update

Conversation

@austenLacy

@austenLacy austenLacy commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

yb_enable_inplace_index_update incorrectly treats primary-key updates on unique secondary indexes as safe for in-place index-row updates. This is false when the unique index uses NULLs-distinct semantics and any unique-index key column is NULL, because the hidden ybuniqueidxkeysuffix is part of the index row key and is derived from the base row ybctid. A base table PK update changes the base row ybctid, so the hidden unique suffix changes too.

Reproducible example:

SET yb_enable_inplace_index_update = on;

CREATE TABLE idx_test (
 c1 bigint,
 id bigint,
 foo smallint DEFAULT 0 NOT NULL,
 some_hash bytea,
 is_not_deleted boolean,
 PRIMARY KEY ((c1) HASH, id ASC, foo ASC)
);

CREATE UNIQUE INDEX idx_test_uq
ON idx_test USING lsm (c1 HASH, some_hash ASC)
WHERE is_not_deleted;

INSERT INTO idx_test (c1, id, foo, some_hash, is_not_deleted)
VALUES (1, 1, 0, NULL, true);

UPDATE idx_test SET foo = 1
WHERE c1 = 1 AND id = 1 AND foo = 0;

SELECT yb_index_check('idx_test_uq'::regclass);

Consistency check immediately fails

yugabyte=# SELECT yb_index_check('idx_test_uq'::regclass);
ERROR:  ybuniqueidxkeysuffix and ybbasectid mismatch
DETAIL:  index: 'idx_test_uq', ybbasectid: '\x47eda949800000000000000121498000000000000001488000000121'

The fix is to essentially force the DELETE+INSERT for this scenario.

With yb_enable_inplace_index_update = off the issue is not reproducible with the above example.

…te` is set to on.

`yb_enable_inplace_index_update` incorrectly treats primary-key updates on unique secondary
indexes as safe for in-place index-row updates. This is false when the unique index uses
NULLs-distinct semantics and any unique-index key column is NULL, because the hidden
`ybuniqueidxkeysuffix` is part of the index row key and is derived from the base row ybctid.
A base table PK update changes the base row ybctid, so the hidden unique suffix changes too.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request ensures that primary key updates on unique indexes with NULL key columns are modeled as DELETE and INSERT operations rather than in-place updates, because the index row key suffix depends on the base table's ybctid. Feedback on the changes suggests switching to a short-lived per-tuple memory context in the newly introduced YbUniqueIndexKeyHasNull function to prevent potential memory leaks during large bulk updates when calling FormIndexDatum.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread src/postgres/src/backend/executor/execIndexing.c
@netlify

netlify Bot commented Jun 10, 2026

Copy link
Copy Markdown

Deploy Preview for infallible-bardeen-164bc9 ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit a77d5f6
🔍 Latest deploy log https://app.netlify.com/projects/infallible-bardeen-164bc9/deploys/6a29bf5e0dda260008be144f
😎 Deploy Preview https://deploy-preview-32126--infallible-bardeen-164bc9.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@austenLacy

Copy link
Copy Markdown
Contributor Author

trigger jenkins

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