Skip to content

Commit fb6de78

Browse files
committed
Fix freezing of a dead HOT-updated tuple
Vacuum calls page-level HOT prune to remove dead HOT tuples before doing liveness checks (HeapTupleSatisfiesVacuum) on the remaining tuples. But concurrent transaction commit/abort may turn DEAD some of the HOT tuples that survived the prune, before HeapTupleSatisfiesVacuum tests them. This happens to activate the code that decides to freeze the tuple ... which resuscitates it, duplicating data. (This is especially bad if there's any unique constraints, because those are now internally violated due to the duplicate entries, though you won't know until you try to REINDEX or dump/restore the table.) One possible fix would be to simply skip doing anything to the tuple, and hope that the next HOT prune would remove it. But there is a problem: if the tuple is older than freeze horizon, this would leave an unfrozen XID behind, and if no HOT prune happens to clean it up before the containing pg_clog segment is truncated away, it'd later cause an error when the XID is looked up. Fix the problem by having the tuple freezing routines cope with the situation: don't freeze the tuple (and keep it dead). In the cases that the XID is older than the freeze age, set the HEAP_XMAX_COMMITTED flag so that there is no need to look up the XID in pg_clog later on. An isolation test is included, authored by Michael Paquier, loosely based on Daniel Wood's original reproducer. It only tests one particular scenario, though, not all the possible ways for this problem to surface; it be good to have a more reliable way to test this more fully, but it'd require more work. In message https://postgr.es/m/20170911140103.5akxptyrwgpc25bw@alvherre.pgsql I outlined another test case (more closely matching Dan Wood's) that exposed a few more ways for the problem to occur. Backpatch all the way back to 9.3, where this problem was introduced by multixact juggling. In branches 9.3 and 9.4, this includes a backpatch of commit e5ff9fefcd50 (of 9.5 era), since the original is not correctable without matching the coding pattern in 9.5 up. Reported-by: Daniel Wood Diagnosed-by: Daniel Wood Reviewed-by: Yi Wen Wong, Michaël Paquier Discussion: https://postgr.es/m/E5711E62-8FDF-4DCA-A888-C200BF6B5742@amazon.com
1 parent ad56dbd commit fb6de78

File tree

5 files changed

+173
-25
lines changed

5 files changed

+173
-25
lines changed

src/backend/access/heap/heapam.c

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6080,14 +6080,23 @@ FreezeMultiXactId(MultiXactId multi, uint16 t_infomask,
60806080
Assert(TransactionIdIsValid(xid));
60816081

60826082
/*
6083-
* If the xid is older than the cutoff, it has to have aborted,
6084-
* otherwise the tuple would have gotten pruned away.
6083+
* The updating transaction cannot possibly be still running, but
6084+
* verify whether it has committed, and request to set the
6085+
* COMMITTED flag if so. (We normally don't see any tuples in
6086+
* this state, because they are removed by page pruning before we
6087+
* try to freeze the page; but this can happen if the updating
6088+
* transaction commits after the page is pruned but before
6089+
* HeapTupleSatisfiesVacuum).
60856090
*/
60866091
if (TransactionIdPrecedes(xid, cutoff_xid))
60876092
{
6088-
Assert(!TransactionIdDidCommit(xid));
6089-
*flags |= FRM_INVALIDATE_XMAX;
6090-
xid = InvalidTransactionId; /* not strictly necessary */
6093+
if (TransactionIdDidCommit(xid))
6094+
*flags = FRM_MARK_COMMITTED | FRM_RETURN_IS_XID;
6095+
else
6096+
{
6097+
*flags |= FRM_INVALIDATE_XMAX;
6098+
xid = InvalidTransactionId; /* not strictly necessary */
6099+
}
60916100
}
60926101
else
60936102
{
@@ -6160,13 +6169,16 @@ FreezeMultiXactId(MultiXactId multi, uint16 t_infomask,
61606169
/*
61616170
* It's an update; should we keep it? If the transaction is known
61626171
* aborted or crashed then it's okay to ignore it, otherwise not.
6163-
* Note that an updater older than cutoff_xid cannot possibly be
6164-
* committed, because HeapTupleSatisfiesVacuum would have returned
6165-
* HEAPTUPLE_DEAD and we would not be trying to freeze the tuple.
61666172
*
61676173
* As with all tuple visibility routines, it's critical to test
61686174
* TransactionIdIsInProgress before TransactionIdDidCommit,
61696175
* because of race conditions explained in detail in tqual.c.
6176+
*
6177+
* We normally don't see committed updating transactions earlier
6178+
* than the cutoff xid, because they are removed by page pruning
6179+
* before we try to freeze the page; but it can happen if the
6180+
* updating transaction commits after the page is pruned but
6181+
* before HeapTupleSatisfiesVacuum.
61706182
*/
61716183
if (TransactionIdIsCurrentTransactionId(xid) ||
61726184
TransactionIdIsInProgress(xid))
@@ -6191,13 +6203,6 @@ FreezeMultiXactId(MultiXactId multi, uint16 t_infomask,
61916203
* we can ignore it.
61926204
*/
61936205

6194-
/*
6195-
* Since the tuple wasn't marked HEAPTUPLE_DEAD by vacuum, the
6196-
* update Xid cannot possibly be older than the xid cutoff.
6197-
*/
6198-
Assert(!TransactionIdIsValid(update_xid) ||
6199-
!TransactionIdPrecedes(update_xid, cutoff_xid));
6200-
62016206
/*
62026207
* If we determined that it's an Xid corresponding to an update
62036208
* that must be retained, additionally add it to the list of
@@ -6274,7 +6279,10 @@ FreezeMultiXactId(MultiXactId multi, uint16 t_infomask,
62746279
*
62756280
* It is assumed that the caller has checked the tuple with
62766281
* HeapTupleSatisfiesVacuum() and determined that it is not HEAPTUPLE_DEAD
6277-
* (else we should be removing the tuple, not freezing it).
6282+
* (else we should be removing the tuple, not freezing it). However, note
6283+
* that we don't remove HOT tuples even if they are dead, and it'd be incorrect
6284+
* to freeze them (because that would make them visible), so we mark them as
6285+
* update-committed, and needing further freezing later on.
62786286
*
62796287
* NB: cutoff_xid *must* be <= the current global xmin, to ensure that any
62806288
* XID older than it could neither be running nor seen as running by any
@@ -6379,7 +6387,18 @@ heap_prepare_freeze_tuple(HeapTupleHeader tuple, TransactionId cutoff_xid,
63796387
else if (TransactionIdIsNormal(xid) &&
63806388
TransactionIdPrecedes(xid, cutoff_xid))
63816389
{
6382-
freeze_xmax = true;
6390+
/*
6391+
* Must freeze regular XIDs older than the cutoff. We must not freeze
6392+
* a HOT-updated tuple, though; doing so would bring it back to life.
6393+
*/
6394+
if (HeapTupleHeaderIsHotUpdated(tuple))
6395+
{
6396+
frz->t_infomask |= HEAP_XMAX_COMMITTED;
6397+
changed = true;
6398+
/* must not freeze */
6399+
}
6400+
else
6401+
freeze_xmax = true;
63836402
}
63846403

63856404
if (freeze_xmax)

src/backend/commands/vacuumlazy.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,15 +1722,15 @@ lazy_record_dead_tuple(LVRelStats *vacrelstats,
17221722
ItemPointer itemptr)
17231723
{
17241724
/*
1725-
* The array shouldn't overflow under normal behavior, but perhaps it
1726-
* could if we are given a really small maintenance_work_mem. In that
1727-
* case, just forget the last few tuples (we'll get 'em next time).
1725+
* The array must never overflow, since we rely on all deletable tuples
1726+
* being removed; inability to remove a tuple might cause an old XID to
1727+
* persist beyond the freeze limit, which could be disastrous later on.
17281728
*/
1729-
if (vacrelstats->num_dead_tuples < vacrelstats->max_dead_tuples)
1730-
{
1731-
vacrelstats->dead_tuples[vacrelstats->num_dead_tuples] = *itemptr;
1732-
vacrelstats->num_dead_tuples++;
1733-
}
1729+
if (vacrelstats->num_dead_tuples >= vacrelstats->max_dead_tuples)
1730+
elog(ERROR, "dead tuple array overflow");
1731+
1732+
vacrelstats->dead_tuples[vacrelstats->num_dead_tuples] = *itemptr;
1733+
vacrelstats->num_dead_tuples++;
17341734
}
17351735

17361736
/*
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
Parsed test spec with 2 sessions
2+
3+
starting permutation: s1_update s1_commit s1_vacuum s2_key_share s2_commit
4+
step s1_update: UPDATE tab_freeze SET x = x + 1 WHERE id = 3;
5+
step s1_commit: COMMIT;
6+
step s1_vacuum: VACUUM FREEZE tab_freeze;
7+
step s2_key_share: SELECT id FROM tab_freeze WHERE id = 3 FOR KEY SHARE;
8+
id
9+
10+
3
11+
step s2_commit: COMMIT;
12+
13+
starting permutation: s1_update s1_commit s2_key_share s1_vacuum s2_commit
14+
step s1_update: UPDATE tab_freeze SET x = x + 1 WHERE id = 3;
15+
step s1_commit: COMMIT;
16+
step s2_key_share: SELECT id FROM tab_freeze WHERE id = 3 FOR KEY SHARE;
17+
id
18+
19+
3
20+
step s1_vacuum: VACUUM FREEZE tab_freeze;
21+
step s2_commit: COMMIT;
22+
23+
starting permutation: s1_update s1_commit s2_key_share s2_commit s1_vacuum
24+
step s1_update: UPDATE tab_freeze SET x = x + 1 WHERE id = 3;
25+
step s1_commit: COMMIT;
26+
step s2_key_share: SELECT id FROM tab_freeze WHERE id = 3 FOR KEY SHARE;
27+
id
28+
29+
3
30+
step s2_commit: COMMIT;
31+
step s1_vacuum: VACUUM FREEZE tab_freeze;
32+
33+
starting permutation: s1_update s2_key_share s1_commit s1_vacuum s2_commit
34+
step s1_update: UPDATE tab_freeze SET x = x + 1 WHERE id = 3;
35+
step s2_key_share: SELECT id FROM tab_freeze WHERE id = 3 FOR KEY SHARE;
36+
id
37+
38+
3
39+
step s1_commit: COMMIT;
40+
step s1_vacuum: VACUUM FREEZE tab_freeze;
41+
step s2_commit: COMMIT;
42+
43+
starting permutation: s1_update s2_key_share s1_commit s2_commit s1_vacuum
44+
step s1_update: UPDATE tab_freeze SET x = x + 1 WHERE id = 3;
45+
step s2_key_share: SELECT id FROM tab_freeze WHERE id = 3 FOR KEY SHARE;
46+
id
47+
48+
3
49+
step s1_commit: COMMIT;
50+
step s2_commit: COMMIT;
51+
step s1_vacuum: VACUUM FREEZE tab_freeze;
52+
53+
starting permutation: s1_update s2_key_share s2_commit s1_commit s1_vacuum
54+
step s1_update: UPDATE tab_freeze SET x = x + 1 WHERE id = 3;
55+
step s2_key_share: SELECT id FROM tab_freeze WHERE id = 3 FOR KEY SHARE;
56+
id
57+
58+
3
59+
step s2_commit: COMMIT;
60+
step s1_commit: COMMIT;
61+
step s1_vacuum: VACUUM FREEZE tab_freeze;
62+
63+
starting permutation: s2_key_share s1_update s1_commit s1_vacuum s2_commit
64+
step s2_key_share: SELECT id FROM tab_freeze WHERE id = 3 FOR KEY SHARE;
65+
id
66+
67+
3
68+
step s1_update: UPDATE tab_freeze SET x = x + 1 WHERE id = 3;
69+
step s1_commit: COMMIT;
70+
step s1_vacuum: VACUUM FREEZE tab_freeze;
71+
step s2_commit: COMMIT;
72+
73+
starting permutation: s2_key_share s1_update s1_commit s2_commit s1_vacuum
74+
step s2_key_share: SELECT id FROM tab_freeze WHERE id = 3 FOR KEY SHARE;
75+
id
76+
77+
3
78+
step s1_update: UPDATE tab_freeze SET x = x + 1 WHERE id = 3;
79+
step s1_commit: COMMIT;
80+
step s2_commit: COMMIT;
81+
step s1_vacuum: VACUUM FREEZE tab_freeze;
82+
83+
starting permutation: s2_key_share s1_update s2_commit s1_commit s1_vacuum
84+
step s2_key_share: SELECT id FROM tab_freeze WHERE id = 3 FOR KEY SHARE;
85+
id
86+
87+
3
88+
step s1_update: UPDATE tab_freeze SET x = x + 1 WHERE id = 3;
89+
step s2_commit: COMMIT;
90+
step s1_commit: COMMIT;
91+
step s1_vacuum: VACUUM FREEZE tab_freeze;
92+
93+
starting permutation: s2_key_share s2_commit s1_update s1_commit s1_vacuum
94+
step s2_key_share: SELECT id FROM tab_freeze WHERE id = 3 FOR KEY SHARE;
95+
id
96+
97+
3
98+
step s2_commit: COMMIT;
99+
step s1_update: UPDATE tab_freeze SET x = x + 1 WHERE id = 3;
100+
step s1_commit: COMMIT;
101+
step s1_vacuum: VACUUM FREEZE tab_freeze;

src/test/isolation/isolation_schedule

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ test: lock-committed-keyupdate
3535
test: update-locked-tuple
3636
test: propagate-lock-delete
3737
test: tuplelock-conflict
38+
test: freeze-the-dead
3839
test: nowait
3940
test: nowait-2
4041
test: nowait-3
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Test for interactions of tuple freezing with dead, as well as recently-dead
2+
# tuples using multixacts via FOR KEY SHARE.
3+
setup
4+
{
5+
CREATE TABLE tab_freeze (
6+
id int PRIMARY KEY,
7+
name char(3),
8+
x int);
9+
INSERT INTO tab_freeze VALUES (1, '111', 0);
10+
INSERT INTO tab_freeze VALUES (3, '333', 0);
11+
}
12+
13+
teardown
14+
{
15+
DROP TABLE tab_freeze;
16+
}
17+
18+
session "s1"
19+
setup { BEGIN; }
20+
step "s1_update" { UPDATE tab_freeze SET x = x + 1 WHERE id = 3; }
21+
step "s1_commit" { COMMIT; }
22+
step "s1_vacuum" { VACUUM FREEZE tab_freeze; }
23+
24+
session "s2"
25+
setup { BEGIN; }
26+
step "s2_key_share" { SELECT id FROM tab_freeze WHERE id = 3 FOR KEY SHARE; }
27+
step "s2_commit" { COMMIT; }

0 commit comments

Comments
 (0)