Skip to content

Commit dee21ea

Browse files
committed
Add missing EPQ recheck for TID Scan
The EvalPlanQual recheck for TID Scan wasn't rechecking the TID qual still passed after following update chains. This could result in tuples being updated or deleted by plans using TID Scans where the ctid of the new (updated) tuple no longer matches the clause of the scan. This isn't desired behavior, and isn't consistent with what would happen if the chosen plan had used an Index or Seq Scan, and that could lead to hard to predict behavior for scans that contain TID quals and other quals as the planner has freedom to choose TID or some other scan method for such queries, and the chosen plan could change at any moment. Here we fix this by properly implementing the recheck function for TID Scans. Backpatch to 13, oldest supported version Reported-by: Sophie Alpert <pg@sophiebits.com> Author: Sophie Alpert <pg@sophiebits.com> Author: David Rowley <dgrowleyml@gmail.com> Reviewed-by: David Rowley <dgrowleyml@gmail.com> Reviewed-by: Chao Li <li.evan.chao@gmail.com> Discussion: https://postgr.es/m/4a6268ff-3340-453a-9bf5-c98d51a6f729@app.fastmail.com Backpatch-through: 13
1 parent e633fa6 commit dee21ea

File tree

3 files changed

+97
-4
lines changed

3 files changed

+97
-4
lines changed

src/backend/executor/nodeTidscan.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,12 +402,23 @@ TidNext(TidScanState *node)
402402
static bool
403403
TidRecheck(TidScanState *node, TupleTableSlot *slot)
404404
{
405+
ItemPointer match;
406+
407+
/* WHERE CURRENT OF always intends to resolve to the latest tuple */
408+
if (node->tss_isCurrentOf)
409+
return true;
410+
411+
if (node->tss_TidList == NULL)
412+
TidListEval(node);
413+
405414
/*
406-
* XXX shouldn't we check here to make sure tuple matches TID list? In
407-
* runtime-key case this is not certain, is it? However, in the WHERE
408-
* CURRENT OF case it might not match anyway ...
415+
* Binary search the TidList to see if this ctid is mentioned and return
416+
* true if it is.
409417
*/
410-
return true;
418+
match = (ItemPointer) bsearch(&slot->tts_tid, node->tss_TidList,
419+
node->tss_NumTids, sizeof(ItemPointerData),
420+
itemptr_comparator);
421+
return match != NULL;
411422
}
412423

413424

src/test/isolation/expected/eval-plan-qual.out

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,77 @@ subid|id
12181218
(1 row)
12191219

12201220

1221+
starting permutation: tid1 tid2 c1 c2 read
1222+
step tid1: UPDATE accounts SET balance = balance + 100 WHERE ctid = '(0,1)' RETURNING accountid, balance;
1223+
accountid|balance
1224+
---------+-------
1225+
checking | 700
1226+
(1 row)
1227+
1228+
step tid2: UPDATE accounts SET balance = balance + 200 WHERE ctid = '(0,1)' RETURNING accountid, balance; <waiting ...>
1229+
step c1: COMMIT;
1230+
step tid2: <... completed>
1231+
accountid|balance
1232+
---------+-------
1233+
(0 rows)
1234+
1235+
step c2: COMMIT;
1236+
step read: SELECT * FROM accounts ORDER BY accountid;
1237+
accountid|balance|balance2
1238+
---------+-------+--------
1239+
checking | 700| 1400
1240+
savings | 600| 1200
1241+
(2 rows)
1242+
1243+
1244+
starting permutation: tid1 tidsucceed2 c1 c2 read
1245+
step tid1: UPDATE accounts SET balance = balance + 100 WHERE ctid = '(0,1)' RETURNING accountid, balance;
1246+
accountid|balance
1247+
---------+-------
1248+
checking | 700
1249+
(1 row)
1250+
1251+
step tidsucceed2: UPDATE accounts SET balance = balance + 200 WHERE ctid = '(0,1)' OR ctid = '(0,3)' RETURNING accountid, balance; <waiting ...>
1252+
step c1: COMMIT;
1253+
step tidsucceed2: <... completed>
1254+
accountid|balance
1255+
---------+-------
1256+
checking | 900
1257+
(1 row)
1258+
1259+
step c2: COMMIT;
1260+
step read: SELECT * FROM accounts ORDER BY accountid;
1261+
accountid|balance|balance2
1262+
---------+-------+--------
1263+
checking | 900| 1800
1264+
savings | 600| 1200
1265+
(2 rows)
1266+
1267+
1268+
starting permutation: tid1 tid2 r1 c2 read
1269+
step tid1: UPDATE accounts SET balance = balance + 100 WHERE ctid = '(0,1)' RETURNING accountid, balance;
1270+
accountid|balance
1271+
---------+-------
1272+
checking | 700
1273+
(1 row)
1274+
1275+
step tid2: UPDATE accounts SET balance = balance + 200 WHERE ctid = '(0,1)' RETURNING accountid, balance; <waiting ...>
1276+
step r1: ROLLBACK;
1277+
step tid2: <... completed>
1278+
accountid|balance
1279+
---------+-------
1280+
checking | 800
1281+
(1 row)
1282+
1283+
step c2: COMMIT;
1284+
step read: SELECT * FROM accounts ORDER BY accountid;
1285+
accountid|balance|balance2
1286+
---------+-------+--------
1287+
checking | 800| 1600
1288+
savings | 600| 1200
1289+
(2 rows)
1290+
1291+
12211292
starting permutation: simplepartupdate conditionalpartupdate c1 c2 read_part
12221293
step simplepartupdate:
12231294
update parttbl set b = b + 10;

src/test/isolation/specs/eval-plan-qual.spec

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ step upsert1 {
9999
WHERE NOT EXISTS (SELECT 1 FROM upsert);
100100
}
101101

102+
# Tests for Tid Scan
103+
step tid1 { UPDATE accounts SET balance = balance + 100 WHERE ctid = '(0,1)' RETURNING accountid, balance; }
104+
102105
# tests with table p check inheritance cases:
103106
# readp1/writep1/readp2 tests a bug where nodeLockRows did the wrong thing
104107
# when the first updated tuple was in a non-first child table.
@@ -241,6 +244,10 @@ step updateforcip3 {
241244
step wrtwcte { UPDATE table_a SET value = 'tableAValue2' WHERE id = 1; }
242245
step wrjt { UPDATE jointest SET data = 42 WHERE id = 7; }
243246

247+
step tid2 { UPDATE accounts SET balance = balance + 200 WHERE ctid = '(0,1)' RETURNING accountid, balance; }
248+
# here, recheck succeeds; (0,3) is the id that step tid1 will assign
249+
step tidsucceed2 { UPDATE accounts SET balance = balance + 200 WHERE ctid = '(0,1)' OR ctid = '(0,3)' RETURNING accountid, balance; }
250+
244251
step conditionalpartupdate {
245252
update parttbl set c = -c where b < 10;
246253
}
@@ -392,6 +399,10 @@ permutation wrtwcte readwcte c1 c2
392399
permutation wrjt selectjoinforupdate c2 c1
393400
permutation wrjt selectresultforupdate c2 c1
394401
permutation wrtwcte multireadwcte c1 c2
402+
permutation tid1 tid2 c1 c2 read
403+
permutation tid1 tidsucceed2 c1 c2 read
404+
# test that a rollback on s1 has s2 perform the update on the original row
405+
permutation tid1 tid2 r1 c2 read
395406

396407
permutation simplepartupdate conditionalpartupdate c1 c2 read_part
397408
permutation simplepartupdate complexpartupdate c1 c2 read_part

0 commit comments

Comments
 (0)