[BugFix] PK-index fast path ignores snapshot version, violating REPEATABLE READ isolation
Bug Description
Severity: CRITICAL
Component: BE Storage Engine
Source file: be/src/storage/tablet_reader.cpp (line 260)
Root cause: The PK-index fast path reads the current live mapping after the snapshot version/GTID is captured at query start. The snapshot version returned by the PK lookup is ignored, allowing a row from a later version to be returned to a query that should see an earlier snapshot.
Concrete failure scenario: Within a BEGIN / COMMIT transaction on a PRIMARY KEY table, a second SELECT returns rows committed by a concurrent session after the snapshot was established, violating REPEATABLE READ isolation. The same test on a UNIQUE KEY table correctly preserves isolation.
Steps to Reproduce
-- Setup
CREATE DATABASE IF NOT EXISTS test_pk_iso;
CREATE TABLE t_pk (id INT, version_num INT, marker VARCHAR(50))
PRIMARY KEY(id) DISTRIBUTED BY HASH(id) BUCKETS 1
PROPERTIES ('replication_num'='1');
INSERT INTO t_pk
SELECT generate_series AS id, 1 AS version_num, 'ORIGINAL' AS marker
FROM TABLE(generate_series(1, 100000));-- Session 1 (establishes snapshot):
BEGIN;
SELECT 'first_read' AS tag, version_num, COUNT(*) AS cnt
FROM t_pk WHERE id BETWEEN 1 AND 100 GROUP BY version_num;
SELECT SLEEP(3);
SELECT 'second_read' AS tag, version_num, COUNT(*) AS cnt
FROM t_pk WHERE id BETWEEN 1 AND 100 GROUP BY version_num;
COMMIT;
-- Session 2 (fires ~1s after BEGIN, during SLEEP):
UPDATE t_pk SET version_num=55, marker='V55' WHERE id BETWEEN 1 AND 100;Actual Behavior
first_read: version_num=1, cnt=100 ✓ (correct)
second_read: version_num=55, cnt=100 ✗ (BUG — sees post-snapshot update)Reproduced in 10/10 trials (100% reproducibility).
Expected Behavior
Within a REPEATABLE READ transaction, the second SELECT should return version_num=1 for all 100 rows — snapshot was established at BEGIN before the concurrent UPDATE committed.
Verification
Control test: identical scenario on a UNIQUE KEY table consistently returns version_num=1 in the second read (isolation preserved). The isolation violation is PK-table specific, confirming the bug is in the PK-index fast path at tablet_reader.cpp:260.
This bug was identified and reproduced by AI-assisted code review.
What type of issue is this:
- BugFix
Does this entail a change in behavior?
- Yes, this issue will result in a change in behavior.
Type of change:
- Miscellaneous: correctness bug fix
Checklist
- Fix needs a regression test
- Fix may need user documentation update
Test Environment
| Tested Version | main-4d6435b |
| Cluster | 1 FE, 3 BE, shared-nothing mode |
Source: StarRocks/starrocks