Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c5eabaf

Browse files
committed
Ensure that CLUSTER leaves the toast table and index with consistent names,
by renaming the new copies after the catalog games.
1 parent a303e4d commit c5eabaf

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/backend/commands/cluster.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $PostgreSQL: pgsql/src/backend/commands/cluster.c,v 1.177 2008/06/19 00:46:04 alvherre Exp $
14+
* $PostgreSQL: pgsql/src/backend/commands/cluster.c,v 1.178 2008/10/14 17:19:50 alvherre Exp $
1515
*
1616
*-------------------------------------------------------------------------
1717
*/
@@ -29,6 +29,7 @@
2929
#include "catalog/index.h"
3030
#include "catalog/indexing.h"
3131
#include "catalog/namespace.h"
32+
#include "catalog/pg_namespace.h"
3233
#include "catalog/toasting.h"
3334
#include "commands/cluster.h"
3435
#include "commands/tablecmds.h"
@@ -568,6 +569,7 @@ rebuild_relation(Relation OldHeap, Oid indexOid)
568569
char NewHeapName[NAMEDATALEN];
569570
TransactionId frozenXid;
570571
ObjectAddress object;
572+
Relation newrel;
571573

572574
/* Mark the correct index as clustered */
573575
mark_index_clustered(OldHeap, indexOid);
@@ -622,6 +624,35 @@ rebuild_relation(Relation OldHeap, Oid indexOid)
622624
* because reindex_relation does it.
623625
*/
624626
reindex_relation(tableOid, false);
627+
628+
/*
629+
* At this point, everything is kosher except that the toast table's name
630+
* corresponds to the temporary table. The name is irrelevant to
631+
* the backend because it's referenced by OID, but users looking at the
632+
* catalogs could be confused. Rename it to prevent this problem.
633+
*
634+
* Note no lock required on the relation, because we already hold an
635+
* exclusive lock on it.
636+
*/
637+
newrel = heap_open(tableOid, NoLock);
638+
if (OidIsValid(newrel->rd_rel->reltoastrelid))
639+
{
640+
char NewToastName[NAMEDATALEN];
641+
Relation toastrel;
642+
643+
/* rename the toast table ... */
644+
snprintf(NewToastName, NAMEDATALEN, "pg_toast_%u", tableOid);
645+
RenameRelationInternal(newrel->rd_rel->reltoastrelid, NewToastName,
646+
PG_TOAST_NAMESPACE);
647+
648+
/* ... and its index too */
649+
toastrel = relation_open(newrel->rd_rel->reltoastrelid, AccessShareLock);
650+
snprintf(NewToastName, NAMEDATALEN, "pg_toast_%u_index", tableOid);
651+
RenameRelationInternal(toastrel->rd_rel->reltoastidxid, NewToastName,
652+
PG_TOAST_NAMESPACE);
653+
relation_close(toastrel, AccessShareLock);
654+
}
655+
relation_close(newrel, NoLock);
625656
}
626657

627658
/*

0 commit comments

Comments
 (0)