Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 83b7584

Browse files
committed
Make CLUSTER lock the old table's toast table before copying data.
We must lock out autovacuuming of the old toast table before computing the OldestXmin horizon we will use. Otherwise, autovacuum could start on the toast table later, compute a later OldestXmin horizon, and remove as DEAD toast tuples that we still need (because we think their parent tuples are only RECENTLY_DEAD). Per further thought about bug #5998.
1 parent 5a71b64 commit 83b7584

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/backend/commands/cluster.c

+17
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "miscadmin.h"
3939
#include "optimizer/planner.h"
4040
#include "storage/bufmgr.h"
41+
#include "storage/lmgr.h"
4142
#include "storage/procarray.h"
4243
#include "storage/smgr.h"
4344
#include "utils/acl.h"
@@ -750,6 +751,22 @@ copy_heap_data(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex,
750751
values = (Datum *) palloc(natts * sizeof(Datum));
751752
isnull = (bool *) palloc(natts * sizeof(bool));
752753

754+
/*
755+
* If the OldHeap has a toast table, get lock on the toast table to keep
756+
* it from being vacuumed. This is needed because autovacuum processes
757+
* toast tables independently of their main tables, with no lock on the
758+
* latter. If an autovacuum were to start on the toast table after we
759+
* compute our OldestXmin below, it would use a later OldestXmin, and then
760+
* possibly remove as DEAD toast tuples belonging to main tuples we think
761+
* are only RECENTLY_DEAD. Then we'd fail while trying to copy those
762+
* tuples.
763+
*
764+
* We don't need to open the toast relation here, just lock it. The lock
765+
* will be held till end of transaction.
766+
*/
767+
if (OldHeap->rd_rel->reltoastrelid)
768+
LockRelationOid(OldHeap->rd_rel->reltoastrelid, AccessExclusiveLock);
769+
753770
/*
754771
* We need to log the copied data in WAL iff WAL archiving/streaming is
755772
* enabled AND it's not a WAL-logged rel.

0 commit comments

Comments
 (0)