PostgreSQL Source Code  git master
cluster.c File Reference
#include "postgres.h"
#include "access/amapi.h"
#include "access/heapam.h"
#include "access/multixact.h"
#include "access/relscan.h"
#include "access/tableam.h"
#include "access/toast_internals.h"
#include "access/transam.h"
#include "access/xact.h"
#include "access/xlog.h"
#include "catalog/catalog.h"
#include "catalog/dependency.h"
#include "catalog/heap.h"
#include "catalog/index.h"
#include "catalog/namespace.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_am.h"
#include "catalog/toasting.h"
#include "commands/cluster.h"
#include "commands/progress.h"
#include "commands/tablecmds.h"
#include "commands/vacuum.h"
#include "miscadmin.h"
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/inval.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/pg_rusage.h"
#include "utils/relmapper.h"
#include "utils/snapmgr.h"
#include "utils/syscache.h"
#include "utils/tuplesort.h"
Include dependency graph for cluster.c:

Go to the source code of this file.

Data Structures

struct  RelToCluster
 

Functions

static void rebuild_relation (Relation OldHeap, Oid indexOid, bool verbose)
 
static void copy_table_data (Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex, bool verbose, bool *pSwapToastByContent, TransactionId *pFreezeXid, MultiXactId *pCutoffMulti)
 
static Listget_tables_to_cluster (MemoryContext cluster_context)
 
void cluster (ClusterStmt *stmt, bool isTopLevel)
 
void cluster_rel (Oid tableOid, Oid indexOid, int options)
 
void check_index_is_clusterable (Relation OldHeap, Oid indexOid, bool recheck, LOCKMODE lockmode)
 
void mark_index_clustered (Relation rel, Oid indexOid, bool is_internal)
 
Oid make_new_heap (Oid OIDOldHeap, Oid NewTableSpace, char relpersistence, LOCKMODE lockmode)
 
static void swap_relation_files (Oid r1, Oid r2, bool target_is_pg_class, bool swap_toast_by_content, bool is_internal, TransactionId frozenXid, MultiXactId cutoffMulti, Oid *mapped_tables)
 
void finish_heap_swap (Oid OIDOldHeap, Oid OIDNewHeap, bool is_system_catalog, bool swap_toast_by_content, bool check_constraints, bool is_internal, TransactionId frozenXid, MultiXactId cutoffMulti, char newrelpersistence)
 

Function Documentation

◆ check_index_is_clusterable()

void check_index_is_clusterable ( Relation  OldHeap,
Oid  indexOid,
bool  recheck,
LOCKMODE  lockmode 
)

Definition at line 419 of file cluster.c.

References IndexAmRoutine::amclusterable, ereport, errcode(), ERRCODE_WRONG_OBJECT_TYPE, errmsg(), ERROR, heap_attisnull(), index_close(), index_open(), NoLock, RelationData::rd_indam, RelationData::rd_index, RelationData::rd_indextuple, RelationGetRelationName, and RelationGetRelid.

Referenced by ATExecClusterOn(), and cluster_rel().

420 {
421  Relation OldIndex;
422 
423  OldIndex = index_open(indexOid, lockmode);
424 
425  /*
426  * Check that index is in fact an index on the given relation
427  */
428  if (OldIndex->rd_index == NULL ||
429  OldIndex->rd_index->indrelid != RelationGetRelid(OldHeap))
430  ereport(ERROR,
432  errmsg("\"%s\" is not an index for table \"%s\"",
433  RelationGetRelationName(OldIndex),
434  RelationGetRelationName(OldHeap))));
435 
436  /* Index AM must allow clustering */
437  if (!OldIndex->rd_indam->amclusterable)
438  ereport(ERROR,
439  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
440  errmsg("cannot cluster on index \"%s\" because access method does not support clustering",
441  RelationGetRelationName(OldIndex))));
442 
443  /*
444  * Disallow clustering on incomplete indexes (those that might not index
445  * every row of the relation). We could relax this by making a separate
446  * seqscan pass over the table to copy the missing rows, but that seems
447  * expensive and tedious.
448  */
449  if (!heap_attisnull(OldIndex->rd_indextuple, Anum_pg_index_indpred, NULL))
450  ereport(ERROR,
451  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
452  errmsg("cannot cluster on partial index \"%s\"",
453  RelationGetRelationName(OldIndex))));
454 
455  /*
456  * Disallow if index is left over from a failed CREATE INDEX CONCURRENTLY;
457  * it might well not contain entries for every heap row, or might not even
458  * be internally consistent. (But note that we don't check indcheckxmin;
459  * the worst consequence of following broken HOT chains would be that we
460  * might put recently-dead tuples out-of-order in the new table, and there
461  * is little harm in that.)
462  */
463  if (!OldIndex->rd_index->indisvalid)
464  ereport(ERROR,
465  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
466  errmsg("cannot cluster on invalid index \"%s\"",
467  RelationGetRelationName(OldIndex))));
468 
469  /* Drop relcache refcnt on OldIndex, but keep lock */
470  index_close(OldIndex, NoLock);
471 }
struct IndexAmRoutine * rd_indam
Definition: rel.h:188
int errcode(int sqlerrcode)
Definition: elog.c:610
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:359
struct HeapTupleData * rd_indextuple
Definition: rel.h:176
Form_pg_index rd_index
Definition: rel.h:174
#define ERRCODE_WRONG_OBJECT_TYPE
#define ERROR
Definition: elog.h:43
#define NoLock
Definition: lockdefs.h:34
#define RelationGetRelationName(relation)
Definition: rel.h:490
bool amclusterable
Definition: amapi.h:237
#define ereport(elevel,...)
Definition: elog.h:144
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:158
int errmsg(const char *fmt,...)
Definition: elog.c:821
#define RelationGetRelid(relation)
Definition: rel.h:456
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:132

◆ cluster()

void cluster ( ClusterStmt stmt,
bool  isTopLevel 
)

Definition at line 102 of file cluster.c.

References AccessExclusiveLock, ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, CLUOPT_RECHECK, cluster_rel(), CommitTransactionCommand(), ereport, errcode(), errmsg(), ERROR, get_index_isclustered(), get_relname_relid(), get_tables_to_cluster(), GetTransactionSnapshot(), ClusterStmt::indexname, RelToCluster::indexOid, InvalidOid, lfirst, lfirst_oid, MemoryContextDelete(), NoLock, OidIsValid, ClusterStmt::options, PopActiveSnapshot(), PortalContext, PreventInTransactionBlock(), PushActiveSnapshot(), RangeVarCallbackOwnsTable(), RangeVarGetRelidExtended(), RelationData::rd_rel, ClusterStmt::relation, RELATION_IS_OTHER_TEMP, RelationGetIndexList(), RangeVar::relname, StartTransactionCommand(), table_close(), table_open(), and RelToCluster::tableOid.

Referenced by standard_ProcessUtility(), start_postmaster(), and stop_postmaster().

103 {
104  if (stmt->relation != NULL)
105  {
106  /* This is the single-relation case. */
107  Oid tableOid,
108  indexOid = InvalidOid;
109  Relation rel;
110 
111  /* Find, lock, and check permissions on the table */
112  tableOid = RangeVarGetRelidExtended(stmt->relation,
114  0,
116  rel = table_open(tableOid, NoLock);
117 
118  /*
119  * Reject clustering a remote temp table ... their local buffer
120  * manager is not going to cope.
121  */
122  if (RELATION_IS_OTHER_TEMP(rel))
123  ereport(ERROR,
124  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
125  errmsg("cannot cluster temporary tables of other sessions")));
126 
127  /*
128  * Reject clustering a partitioned table.
129  */
130  if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
131  ereport(ERROR,
132  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
133  errmsg("cannot cluster a partitioned table")));
134 
135  if (stmt->indexname == NULL)
136  {
137  ListCell *index;
138 
139  /* We need to find the index that has indisclustered set. */
140  foreach(index, RelationGetIndexList(rel))
141  {
142  indexOid = lfirst_oid(index);
143  if (get_index_isclustered(indexOid))
144  break;
145  indexOid = InvalidOid;
146  }
147 
148  if (!OidIsValid(indexOid))
149  ereport(ERROR,
150  (errcode(ERRCODE_UNDEFINED_OBJECT),
151  errmsg("there is no previously clustered index for table \"%s\"",
152  stmt->relation->relname)));
153  }
154  else
155  {
156  /*
157  * The index is expected to be in the same namespace as the
158  * relation.
159  */
160  indexOid = get_relname_relid(stmt->indexname,
161  rel->rd_rel->relnamespace);
162  if (!OidIsValid(indexOid))
163  ereport(ERROR,
164  (errcode(ERRCODE_UNDEFINED_OBJECT),
165  errmsg("index \"%s\" for table \"%s\" does not exist",
166  stmt->indexname, stmt->relation->relname)));
167  }
168 
169  /* close relation, keep lock till commit */
170  table_close(rel, NoLock);
171 
172  /* Do the job. */
173  cluster_rel(tableOid, indexOid, stmt->options);
174  }
175  else
176  {
177  /*
178  * This is the "multi relation" case. We need to cluster all tables
179  * that have some index with indisclustered set.
180  */
181  MemoryContext cluster_context;
182  List *rvs;
183  ListCell *rv;
184 
185  /*
186  * We cannot run this form of CLUSTER inside a user transaction block;
187  * we'd be holding locks way too long.
188  */
189  PreventInTransactionBlock(isTopLevel, "CLUSTER");
190 
191  /*
192  * Create special memory context for cross-transaction storage.
193  *
194  * Since it is a child of PortalContext, it will go away even in case
195  * of error.
196  */
197  cluster_context = AllocSetContextCreate(PortalContext,
198  "Cluster",
200 
201  /*
202  * Build the list of relations to cluster. Note that this lives in
203  * cluster_context.
204  */
205  rvs = get_tables_to_cluster(cluster_context);
206 
207  /* Commit to get out of starting transaction */
210 
211  /* Ok, now that we've got them all, cluster them one by one */
212  foreach(rv, rvs)
213  {
214  RelToCluster *rvtc = (RelToCluster *) lfirst(rv);
215 
216  /* Start a new transaction for each relation. */
218  /* functions in indexes may want a snapshot set */
220  /* Do the job. */
221  cluster_rel(rvtc->tableOid, rvtc->indexOid,
222  stmt->options | CLUOPT_RECHECK);
225  }
226 
227  /* Start a new transaction for the cleanup work. */
229 
230  /* Clean up working storage */
231  MemoryContextDelete(cluster_context);
232  }
233 }
void RangeVarCallbackOwnsTable(const RangeVar *relation, Oid relId, Oid oldRelId, void *arg)
Definition: tablecmds.c:15564
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:212
#define AllocSetContextCreate
Definition: memutils.h:170
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void CommitTransactionCommand(void)
Definition: xact.c:2947
int errcode(int sqlerrcode)
Definition: elog.c:610
void PopActiveSnapshot(void)
Definition: snapmgr.c:759
Form_pg_class rd_rel
Definition: rel.h:109
unsigned int Oid
Definition: postgres_ext.h:31
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:250
#define OidIsValid(objectId)
Definition: c.h:652
void cluster_rel(Oid tableOid, Oid indexOid, int options)
Definition: cluster.c:253
Oid tableOid
Definition: cluster.c:65
MemoryContext PortalContext
Definition: mcxt.c:53
Definition: type.h:89
char * relname
Definition: primnodes.h:68
char * indexname
Definition: parsenodes.h:3217
#define ERROR
Definition: elog.h:43
Oid get_relname_relid(const char *relname, Oid relnamespace)
Definition: lsyscache.c:1797
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
#define NoLock
Definition: lockdefs.h:34
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:680
void PreventInTransactionBlock(bool isTopLevel, const char *stmtType)
Definition: xact.c:3380
Oid RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, uint32 flags, RangeVarGetRelidCallback callback, void *callback_arg)
Definition: namespace.c:236
static List * get_tables_to_cluster(MemoryContext cluster_context)
Definition: cluster.c:1513
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:144
#define lfirst(lc)
Definition: pg_list.h:169
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:593
void StartTransactionCommand(void)
Definition: xact.c:2846
bool get_index_isclustered(Oid index_oid)
Definition: lsyscache.c:3380
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4514
Oid indexOid
Definition: cluster.c:66
#define AccessExclusiveLock
Definition: lockdefs.h:45
int errmsg(const char *fmt,...)
Definition: elog.c:821
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
RangeVar * relation
Definition: parsenodes.h:3216
Definition: pg_list.h:50
#define lfirst_oid(lc)
Definition: pg_list.h:171

◆ cluster_rel()

void cluster_rel ( Oid  tableOid,
Oid  indexOid,
int  options 
)

Definition at line 253 of file cluster.c.

References AccessExclusiveLock, CHECK_FOR_INTERRUPTS, check_index_is_clusterable(), CheckTableNotInUse(), CLUOPT_RECHECK, CLUOPT_VERBOSE, ereport, errcode(), errmsg(), ERROR, get_index_isclustered(), GetUserId(), ObjectIdGetDatum, OidIsValid, pg_class_ownercheck(), pgstat_progress_end_command(), pgstat_progress_start_command(), pgstat_progress_update_param(), PROGRESS_CLUSTER_COMMAND, PROGRESS_CLUSTER_COMMAND_CLUSTER, PROGRESS_CLUSTER_COMMAND_VACUUM_FULL, PROGRESS_COMMAND_CLUSTER, RelationData::rd_rel, rebuild_relation(), relation_close(), RELATION_IS_OTHER_TEMP, RelationIsPopulated, RELOID, SearchSysCacheExists1, TransferPredicateLocksToHeapRelation(), try_relation_open(), and verbose.

Referenced by cluster(), and vacuum_rel().

254 {
255  Relation OldHeap;
256  bool verbose = ((options & CLUOPT_VERBOSE) != 0);
257  bool recheck = ((options & CLUOPT_RECHECK) != 0);
258 
259  /* Check for user-requested abort. */
261 
263  if (OidIsValid(indexOid))
266  else
269 
270  /*
271  * We grab exclusive access to the target rel and index for the duration
272  * of the transaction. (This is redundant for the single-transaction
273  * case, since cluster() already did it.) The index lock is taken inside
274  * check_index_is_clusterable.
275  */
276  OldHeap = try_relation_open(tableOid, AccessExclusiveLock);
277 
278  /* If the table has gone away, we can skip processing it */
279  if (!OldHeap)
280  {
282  return;
283  }
284 
285  /*
286  * Since we may open a new transaction for each relation, we have to check
287  * that the relation still is what we think it is.
288  *
289  * If this is a single-transaction CLUSTER, we can skip these tests. We
290  * *must* skip the one on indisclustered since it would reject an attempt
291  * to cluster a not-previously-clustered index.
292  */
293  if (recheck)
294  {
295  /* Check that the user still owns the relation */
296  if (!pg_class_ownercheck(tableOid, GetUserId()))
297  {
300  return;
301  }
302 
303  /*
304  * Silently skip a temp table for a remote session. Only doing this
305  * check in the "recheck" case is appropriate (which currently means
306  * somebody is executing a database-wide CLUSTER), because there is
307  * another check in cluster() which will stop any attempt to cluster
308  * remote temp tables by name. There is another check in cluster_rel
309  * which is redundant, but we leave it for extra safety.
310  */
311  if (RELATION_IS_OTHER_TEMP(OldHeap))
312  {
315  return;
316  }
317 
318  if (OidIsValid(indexOid))
319  {
320  /*
321  * Check that the index still exists
322  */
324  {
327  return;
328  }
329 
330  /*
331  * Check that the index is still the one with indisclustered set.
332  */
333  if (!get_index_isclustered(indexOid))
334  {
337  return;
338  }
339  }
340  }
341 
342  /*
343  * We allow VACUUM FULL, but not CLUSTER, on shared catalogs. CLUSTER
344  * would work in most respects, but the index would only get marked as
345  * indisclustered in the current database, leading to unexpected behavior
346  * if CLUSTER were later invoked in another database.
347  */
348  if (OidIsValid(indexOid) && OldHeap->rd_rel->relisshared)
349  ereport(ERROR,
350  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
351  errmsg("cannot cluster a shared catalog")));
352 
353  /*
354  * Don't process temp tables of other backends ... their local buffer
355  * manager is not going to cope.
356  */
357  if (RELATION_IS_OTHER_TEMP(OldHeap))
358  {
359  if (OidIsValid(indexOid))
360  ereport(ERROR,
361  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
362  errmsg("cannot cluster temporary tables of other sessions")));
363  else
364  ereport(ERROR,
365  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
366  errmsg("cannot vacuum temporary tables of other sessions")));
367  }
368 
369  /*
370  * Also check for active uses of the relation in the current transaction,
371  * including open scans and pending AFTER trigger events.
372  */
373  CheckTableNotInUse(OldHeap, OidIsValid(indexOid) ? "CLUSTER" : "VACUUM");
374 
375  /* Check heap and index are valid to cluster on */
376  if (OidIsValid(indexOid))
377  check_index_is_clusterable(OldHeap, indexOid, recheck, AccessExclusiveLock);
378 
379  /*
380  * Quietly ignore the request if this is a materialized view which has not
381  * been populated from its query. No harm is done because there is no data
382  * to deal with, and we don't want to throw an error if this is part of a
383  * multi-relation request -- for example, CLUSTER was run on the entire
384  * database.
385  */
386  if (OldHeap->rd_rel->relkind == RELKIND_MATVIEW &&
387  !RelationIsPopulated(OldHeap))
388  {
391  return;
392  }
393 
394  /*
395  * All predicate locks on the tuples or pages are about to be made
396  * invalid, because we move tuples around. Promote them to relation
397  * locks. Predicate locks on indexes will be promoted when they are
398  * reindexed.
399  */
401 
402  /* rebuild_relation does all the dirty work */
403  rebuild_relation(OldHeap, indexOid, verbose);
404 
405  /* NB: rebuild_relation does table_close() on OldHeap */
406 
408 }
#define RelationIsPopulated(relation)
Definition: rel.h:612
Oid GetUserId(void)
Definition: miscinit.c:476
void pgstat_progress_start_command(ProgressCommandType cmdtype, Oid relid)
Definition: pgstat.c:3356
void pgstat_progress_update_param(int index, int64 val)
Definition: pgstat.c:3377
int errcode(int sqlerrcode)
Definition: elog.c:610
Form_pg_class rd_rel
Definition: rel.h:109
#define OidIsValid(objectId)
Definition: c.h:652
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:89
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:183
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
static void rebuild_relation(Relation OldHeap, Oid indexOid, bool verbose)
Definition: cluster.c:552
#define PROGRESS_CLUSTER_COMMAND_VACUUM_FULL
Definition: progress.h:75
#define PROGRESS_CLUSTER_COMMAND_CLUSTER
Definition: progress.h:74
void CheckTableNotInUse(Relation rel, const char *stmt)
Definition: tablecmds.c:3592
#define PROGRESS_CLUSTER_COMMAND
Definition: progress.h:55
void TransferPredicateLocksToHeapRelation(Relation relation)
Definition: predicate.c:3075
static int verbose
void pgstat_progress_end_command(void)
Definition: pgstat.c:3428
#define ereport(elevel,...)
Definition: elog.h:144
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:593
bool pg_class_ownercheck(Oid class_oid, Oid roleid)
Definition: aclchk.c:4687
bool get_index_isclustered(Oid index_oid)
Definition: lsyscache.c:3380
#define AccessExclusiveLock
Definition: lockdefs.h:45
int errmsg(const char *fmt,...)
Definition: elog.c:821
void check_index_is_clusterable(Relation OldHeap, Oid indexOid, bool recheck, LOCKMODE lockmode)
Definition: cluster.c:419
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99

◆ copy_table_data()

static void copy_table_data ( Oid  OIDNewHeap,
Oid  OIDOldHeap,
Oid  OIDOldIndex,
bool  verbose,
bool pSwapToastByContent,
TransactionId pFreezeXid,
MultiXactId pCutoffMulti 
)
static

Definition at line 731 of file cluster.c.

References AccessExclusiveLock, Assert, CacheInvalidateRelcacheByTuple(), CatalogTupleUpdate(), CommandCounterIncrement(), DEBUG2, elevel, elog, ereport, errdetail(), errmsg(), ERROR, get_namespace_name(), GETSTRUCT, heap_freetuple(), HeapTupleIsValid, index_close(), index_open(), INFO, InvalidOid, LockRelationOid(), MultiXactCutoff, MultiXactIdIsValid, MultiXactIdPrecedes(), NoLock, ObjectIdGetDatum, OidIsValid, OldestXmin, pg_rusage_init(), pg_rusage_show(), PG_USED_FOR_ASSERTS_ONLY, plan_cluster_use_sort(), RelationData::rd_rel, RelationData::rd_toastoid, RelationGetDescr, RelationGetNamespace, RelationGetNumberOfBlocks, RelationGetRelationName, RELOID, RowExclusiveLock, SearchSysCacheCopy1, HeapTupleData::t_self, table_close(), table_open(), table_relation_copy_for_cluster(), TransactionIdIsValid, TransactionIdPrecedes(), and vacuum_set_xid_limits().

Referenced by rebuild_relation().

734 {
735  Relation NewHeap,
736  OldHeap,
737  OldIndex;
738  Relation relRelation;
739  HeapTuple reltup;
740  Form_pg_class relform;
744  TransactionId FreezeXid;
746  bool use_sort;
747  double num_tuples = 0,
748  tups_vacuumed = 0,
749  tups_recently_dead = 0;
750  BlockNumber num_pages;
751  int elevel = verbose ? INFO : DEBUG2;
752  PGRUsage ru0;
753 
754  pg_rusage_init(&ru0);
755 
756  /*
757  * Open the relations we need.
758  */
759  NewHeap = table_open(OIDNewHeap, AccessExclusiveLock);
760  OldHeap = table_open(OIDOldHeap, AccessExclusiveLock);
761  if (OidIsValid(OIDOldIndex))
762  OldIndex = index_open(OIDOldIndex, AccessExclusiveLock);
763  else
764  OldIndex = NULL;
765 
766  /*
767  * Their tuple descriptors should be exactly alike, but here we only need
768  * assume that they have the same number of columns.
769  */
770  oldTupDesc = RelationGetDescr(OldHeap);
771  newTupDesc = RelationGetDescr(NewHeap);
772  Assert(newTupDesc->natts == oldTupDesc->natts);
773 
774  /*
775  * If the OldHeap has a toast table, get lock on the toast table to keep
776  * it from being vacuumed. This is needed because autovacuum processes
777  * toast tables independently of their main tables, with no lock on the
778  * latter. If an autovacuum were to start on the toast table after we
779  * compute our OldestXmin below, it would use a later OldestXmin, and then
780  * possibly remove as DEAD toast tuples belonging to main tuples we think
781  * are only RECENTLY_DEAD. Then we'd fail while trying to copy those
782  * tuples.
783  *
784  * We don't need to open the toast relation here, just lock it. The lock
785  * will be held till end of transaction.
786  */
787  if (OldHeap->rd_rel->reltoastrelid)
788  LockRelationOid(OldHeap->rd_rel->reltoastrelid, AccessExclusiveLock);
789 
790  /*
791  * If both tables have TOAST tables, perform toast swap by content. It is
792  * possible that the old table has a toast table but the new one doesn't,
793  * if toastable columns have been dropped. In that case we have to do
794  * swap by links. This is okay because swap by content is only essential
795  * for system catalogs, and we don't support schema changes for them.
796  */
797  if (OldHeap->rd_rel->reltoastrelid && NewHeap->rd_rel->reltoastrelid)
798  {
799  *pSwapToastByContent = true;
800 
801  /*
802  * When doing swap by content, any toast pointers written into NewHeap
803  * must use the old toast table's OID, because that's where the toast
804  * data will eventually be found. Set this up by setting rd_toastoid.
805  * This also tells toast_save_datum() to preserve the toast value
806  * OIDs, which we want so as not to invalidate toast pointers in
807  * system catalog caches, and to avoid making multiple copies of a
808  * single toast value.
809  *
810  * Note that we must hold NewHeap open until we are done writing data,
811  * since the relcache will not guarantee to remember this setting once
812  * the relation is closed. Also, this technique depends on the fact
813  * that no one will try to read from the NewHeap until after we've
814  * finished writing it and swapping the rels --- otherwise they could
815  * follow the toast pointers to the wrong place. (It would actually
816  * work for values copied over from the old toast table, but not for
817  * any values that we toast which were previously not toasted.)
818  */
819  NewHeap->rd_toastoid = OldHeap->rd_rel->reltoastrelid;
820  }
821  else
822  *pSwapToastByContent = false;
823 
824  /*
825  * Compute xids used to freeze and weed out dead tuples and multixacts.
826  * Since we're going to rewrite the whole table anyway, there's no reason
827  * not to be aggressive about this.
828  */
829  vacuum_set_xid_limits(OldHeap, 0, 0, 0, 0,
830  &OldestXmin, &FreezeXid, NULL, &MultiXactCutoff,
831  NULL);
832 
833  /*
834  * FreezeXid will become the table's new relfrozenxid, and that mustn't go
835  * backwards, so take the max.
836  */
837  if (TransactionIdIsValid(OldHeap->rd_rel->relfrozenxid) &&
838  TransactionIdPrecedes(FreezeXid, OldHeap->rd_rel->relfrozenxid))
839  FreezeXid = OldHeap->rd_rel->relfrozenxid;
840 
841  /*
842  * MultiXactCutoff, similarly, shouldn't go backwards either.
843  */
844  if (MultiXactIdIsValid(OldHeap->rd_rel->relminmxid) &&
845  MultiXactIdPrecedes(MultiXactCutoff, OldHeap->rd_rel->relminmxid))
846  MultiXactCutoff = OldHeap->rd_rel->relminmxid;
847 
848  /*
849  * Decide whether to use an indexscan or seqscan-and-optional-sort to scan
850  * the OldHeap. We know how to use a sort to duplicate the ordering of a
851  * btree index, and will use seqscan-and-sort for that case if the planner
852  * tells us it's cheaper. Otherwise, always indexscan if an index is
853  * provided, else plain seqscan.
854  */
855  if (OldIndex != NULL && OldIndex->rd_rel->relam == BTREE_AM_OID)
856  use_sort = plan_cluster_use_sort(OIDOldHeap, OIDOldIndex);
857  else
858  use_sort = false;
859 
860  /* Log what we're doing */
861  if (OldIndex != NULL && !use_sort)
862  ereport(elevel,
863  (errmsg("clustering \"%s.%s\" using index scan on \"%s\"",
865  RelationGetRelationName(OldHeap),
866  RelationGetRelationName(OldIndex))));
867  else if (use_sort)
868  ereport(elevel,
869  (errmsg("clustering \"%s.%s\" using sequential scan and sort",
871  RelationGetRelationName(OldHeap))));
872  else
873  ereport(elevel,
874  (errmsg("vacuuming \"%s.%s\"",
876  RelationGetRelationName(OldHeap))));
877 
878  /*
879  * Hand of the actual copying to AM specific function, the generic code
880  * cannot know how to deal with visibility across AMs. Note that this
881  * routine is allowed to set FreezeXid / MultiXactCutoff to different
882  * values (e.g. because the AM doesn't use freezing).
883  */
884  table_relation_copy_for_cluster(OldHeap, NewHeap, OldIndex, use_sort,
885  OldestXmin, &FreezeXid, &MultiXactCutoff,
886  &num_tuples, &tups_vacuumed,
887  &tups_recently_dead);
888 
889  /* return selected values to caller, get set as relfrozenxid/minmxid */
890  *pFreezeXid = FreezeXid;
891  *pCutoffMulti = MultiXactCutoff;
892 
893  /* Reset rd_toastoid just to be tidy --- it shouldn't be looked at again */
894  NewHeap->rd_toastoid = InvalidOid;
895 
896  num_pages = RelationGetNumberOfBlocks(NewHeap);
897 
898  /* Log what we did */
899  ereport(elevel,
900  (errmsg("\"%s\": found %.0f removable, %.0f nonremovable row versions in %u pages",
901  RelationGetRelationName(OldHeap),
902  tups_vacuumed, num_tuples,
903  RelationGetNumberOfBlocks(OldHeap)),
904  errdetail("%.0f dead row versions cannot be removed yet.\n"
905  "%s.",
906  tups_recently_dead,
907  pg_rusage_show(&ru0))));
908 
909  if (OldIndex != NULL)
910  index_close(OldIndex, NoLock);
911  table_close(OldHeap, NoLock);
912  table_close(NewHeap, NoLock);
913 
914  /* Update pg_class to reflect the correct values of pages and tuples. */
915  relRelation = table_open(RelationRelationId, RowExclusiveLock);
916 
917  reltup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(OIDNewHeap));
918  if (!HeapTupleIsValid(reltup))
919  elog(ERROR, "cache lookup failed for relation %u", OIDNewHeap);
920  relform = (Form_pg_class) GETSTRUCT(reltup);
921 
922  relform->relpages = num_pages;
923  relform->reltuples = num_tuples;
924 
925  /* Don't update the stats for pg_class. See swap_relation_files. */
926  if (OIDOldHeap != RelationRelationId)
927  CatalogTupleUpdate(relRelation, &reltup->t_self, reltup);
928  else
930 
931  /* Clean up. */
932  heap_freetuple(reltup);
933  table_close(relRelation, RowExclusiveLock);
934 
935  /* Make the update visible */
937 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
bool plan_cluster_use_sort(Oid tableOid, Oid indexOid)
Definition: planner.c:6254
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
uint32 TransactionId
Definition: c.h:521
#define RelationGetDescr(relation)
Definition: rel.h:482
#define INFO
Definition: elog.h:33
void vacuum_set_xid_limits(Relation rel, int freeze_min_age, int freeze_table_age, int multixact_freeze_min_age, int multixact_freeze_table_age, TransactionId *oldestXmin, TransactionId *freezeLimit, TransactionId *xidFullScanLimit, MultiXactId *multiXactCutoff, MultiXactId *mxactFullScanLimit)
Definition: vacuum.c:931
uint32 BlockNumber
Definition: block.h:31
Form_pg_class rd_rel
Definition: rel.h:109
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
#define OidIsValid(objectId)
Definition: c.h:652
void pg_rusage_init(PGRUsage *ru0)
Definition: pg_rusage.c:27
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define DEBUG2
Definition: elog.h:24
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3191
#define NoLock
Definition: lockdefs.h:34
Oid rd_toastoid
Definition: rel.h:233
#define RowExclusiveLock
Definition: lockdefs.h:38
int errdetail(const char *fmt,...)
Definition: elog.c:954
static MultiXactId MultiXactCutoff
Definition: vacuumlazy.c:337
const char * pg_rusage_show(const PGRUsage *ru0)
Definition: pg_rusage.c:40
#define RelationGetRelationName(relation)
Definition: rel.h:490
static TransactionId OldestXmin
Definition: vacuumlazy.c:335
#define MultiXactIdIsValid(multi)
Definition: multixact.h:28
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
static int verbose
static int elevel
Definition: vacuumlazy.c:333
void CommandCounterIncrement(void)
Definition: xact.c:1021
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:211
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:144
TransactionId MultiXactId
Definition: c.h:531
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:746
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3146
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:158
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:174
#define AccessExclusiveLock
Definition: lockdefs.h:45
int errmsg(const char *fmt,...)
Definition: elog.c:821
#define elog(elevel,...)
Definition: elog.h:214
void CacheInvalidateRelcacheByTuple(HeapTuple classTuple)
Definition: inval.c:1314
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:108
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
static void table_relation_copy_for_cluster(Relation OldTable, Relation NewTable, Relation OldIndex, bool use_sort, TransactionId OldestXmin, TransactionId *xid_cutoff, MultiXactId *multi_cutoff, double *num_tuples, double *tups_vacuumed, double *tups_recently_dead)
Definition: tableam.h:1453
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:132
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:122
#define RelationGetNamespace(relation)
Definition: rel.h:497

◆ finish_heap_swap()

void finish_heap_swap ( Oid  OIDOldHeap,
Oid  OIDNewHeap,
bool  is_system_catalog,
bool  swap_toast_by_content,
bool  check_constraints,
bool  is_internal,
TransactionId  frozenXid,
MultiXactId  cutoffMulti,
char  newrelpersistence 
)

Definition at line 1324 of file cluster.c.

References CacheInvalidateCatalog(), CatalogTupleUpdate(), DROP_RESTRICT, elog, ERROR, GETSTRUCT, HeapTupleIsValid, i, NAMEDATALEN, NoLock, ObjectIdGetDatum, OidIsValid, PERFORM_DELETION_INTERNAL, performDeletion(), pgstat_progress_update_param(), PROGRESS_CLUSTER_PHASE, PROGRESS_CLUSTER_PHASE_FINAL_CLEANUP, PROGRESS_CLUSTER_PHASE_REBUILD_INDEX, PROGRESS_CLUSTER_PHASE_SWAP_REL_FILES, RelationData::rd_rel, REINDEX_REL_CHECK_CONSTRAINTS, REINDEX_REL_FORCE_INDEXES_PERMANENT, REINDEX_REL_FORCE_INDEXES_UNLOGGED, REINDEX_REL_SUPPRESS_INDEX_USE, reindex_relation(), relation_close(), RelationClearMissing(), RelationMapRemoveMapping(), RELOID, RenameRelationInternal(), RowExclusiveLock, SearchSysCacheCopy1, snprintf, swap_relation_files(), HeapTupleData::t_self, table_close(), table_open(), and toast_get_valid_index().

Referenced by ATRewriteTables(), rebuild_relation(), and refresh_by_heap_swap().

1332 {
1333  ObjectAddress object;
1334  Oid mapped_tables[4];
1335  int reindex_flags;
1336  int i;
1337 
1338  /* Report that we are now swapping relation files */
1341 
1342  /* Zero out possible results from swapped_relation_files */
1343  memset(mapped_tables, 0, sizeof(mapped_tables));
1344 
1345  /*
1346  * Swap the contents of the heap relations (including any toast tables).
1347  * Also set old heap's relfrozenxid to frozenXid.
1348  */
1349  swap_relation_files(OIDOldHeap, OIDNewHeap,
1350  (OIDOldHeap == RelationRelationId),
1351  swap_toast_by_content, is_internal,
1352  frozenXid, cutoffMulti, mapped_tables);
1353 
1354  /*
1355  * If it's a system catalog, queue a sinval message to flush all catcaches
1356  * on the catalog when we reach CommandCounterIncrement.
1357  */
1358  if (is_system_catalog)
1359  CacheInvalidateCatalog(OIDOldHeap);
1360 
1361  /*
1362  * Rebuild each index on the relation (but not the toast table, which is
1363  * all-new at this point). It is important to do this before the DROP
1364  * step because if we are processing a system catalog that will be used
1365  * during DROP, we want to have its indexes available. There is no
1366  * advantage to the other order anyway because this is all transactional,
1367  * so no chance to reclaim disk space before commit. We do not need a
1368  * final CommandCounterIncrement() because reindex_relation does it.
1369  *
1370  * Note: because index_build is called via reindex_relation, it will never
1371  * set indcheckxmin true for the indexes. This is OK even though in some
1372  * sense we are building new indexes rather than rebuilding existing ones,
1373  * because the new heap won't contain any HOT chains at all, let alone
1374  * broken ones, so it can't be necessary to set indcheckxmin.
1375  */
1376  reindex_flags = REINDEX_REL_SUPPRESS_INDEX_USE;
1377  if (check_constraints)
1378  reindex_flags |= REINDEX_REL_CHECK_CONSTRAINTS;
1379 
1380  /*
1381  * Ensure that the indexes have the same persistence as the parent
1382  * relation.
1383  */
1384  if (newrelpersistence == RELPERSISTENCE_UNLOGGED)
1385  reindex_flags |= REINDEX_REL_FORCE_INDEXES_UNLOGGED;
1386  else if (newrelpersistence == RELPERSISTENCE_PERMANENT)
1387  reindex_flags |= REINDEX_REL_FORCE_INDEXES_PERMANENT;
1388 
1389  /* Report that we are now reindexing relations */
1392 
1393  reindex_relation(OIDOldHeap, reindex_flags, 0);
1394 
1395  /* Report that we are now doing clean up */
1398 
1399  /*
1400  * If the relation being rebuild is pg_class, swap_relation_files()
1401  * couldn't update pg_class's own pg_class entry (check comments in
1402  * swap_relation_files()), thus relfrozenxid was not updated. That's
1403  * annoying because a potential reason for doing a VACUUM FULL is a
1404  * imminent or actual anti-wraparound shutdown. So, now that we can
1405  * access the new relation using its indices, update relfrozenxid.
1406  * pg_class doesn't have a toast relation, so we don't need to update the
1407  * corresponding toast relation. Not that there's little point moving all
1408  * relfrozenxid updates here since swap_relation_files() needs to write to
1409  * pg_class for non-mapped relations anyway.
1410  */
1411  if (OIDOldHeap == RelationRelationId)
1412  {
1413  Relation relRelation;
1414  HeapTuple reltup;
1415  Form_pg_class relform;
1416 
1417  relRelation = table_open(RelationRelationId, RowExclusiveLock);
1418 
1419  reltup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(OIDOldHeap));
1420  if (!HeapTupleIsValid(reltup))
1421  elog(ERROR, "cache lookup failed for relation %u", OIDOldHeap);
1422  relform = (Form_pg_class) GETSTRUCT(reltup);
1423 
1424  relform->relfrozenxid = frozenXid;
1425  relform->relminmxid = cutoffMulti;
1426 
1427  CatalogTupleUpdate(relRelation, &reltup->t_self, reltup);
1428 
1429  table_close(relRelation, RowExclusiveLock);
1430  }
1431 
1432  /* Destroy new heap with old filenode */
1433  object.classId = RelationRelationId;
1434  object.objectId = OIDNewHeap;
1435  object.objectSubId = 0;
1436 
1437  /*
1438  * The new relation is local to our transaction and we know nothing
1439  * depends on it, so DROP_RESTRICT should be OK.
1440  */
1442 
1443  /* performDeletion does CommandCounterIncrement at end */
1444 
1445  /*
1446  * Now we must remove any relation mapping entries that we set up for the
1447  * transient table, as well as its toast table and toast index if any. If
1448  * we fail to do this before commit, the relmapper will complain about new
1449  * permanent map entries being added post-bootstrap.
1450  */
1451  for (i = 0; OidIsValid(mapped_tables[i]); i++)
1452  RelationMapRemoveMapping(mapped_tables[i]);
1453 
1454  /*
1455  * At this point, everything is kosher except that, if we did toast swap
1456  * by links, the toast table's name corresponds to the transient table.
1457  * The name is irrelevant to the backend because it's referenced by OID,
1458  * but users looking at the catalogs could be confused. Rename it to
1459  * prevent this problem.
1460  *
1461  * Note no lock required on the relation, because we already hold an
1462  * exclusive lock on it.
1463  */
1464  if (!swap_toast_by_content)
1465  {
1466  Relation newrel;
1467 
1468  newrel = table_open(OIDOldHeap, NoLock);
1469  if (OidIsValid(newrel->rd_rel->reltoastrelid))
1470  {
1471  Oid toastidx;
1472  char NewToastName[NAMEDATALEN];
1473 
1474  /* Get the associated valid index to be renamed */
1475  toastidx = toast_get_valid_index(newrel->rd_rel->reltoastrelid,
1476  NoLock);
1477 
1478  /* rename the toast table ... */
1479  snprintf(NewToastName, NAMEDATALEN, "pg_toast_%u",
1480  OIDOldHeap);
1481  RenameRelationInternal(newrel->rd_rel->reltoastrelid,
1482  NewToastName, true, false);
1483 
1484  /* ... and its valid index too. */
1485  snprintf(NewToastName, NAMEDATALEN, "pg_toast_%u_index",
1486  OIDOldHeap);
1487 
1488  RenameRelationInternal(toastidx,
1489  NewToastName, true, true);
1490  }
1491  relation_close(newrel, NoLock);
1492  }
1493 
1494  /* if it's not a catalog table, clear any missing attribute settings */
1495  if (!is_system_catalog)
1496  {
1497  Relation newrel;
1498 
1499  newrel = table_open(OIDOldHeap, NoLock);
1500  RelationClearMissing(newrel);
1501  relation_close(newrel, NoLock);
1502  }
1503 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
void pgstat_progress_update_param(int index, int64 val)
Definition: pgstat.c:3377
#define PROGRESS_CLUSTER_PHASE_SWAP_REL_FILES
Definition: progress.h:69
#define REINDEX_REL_SUPPRESS_INDEX_USE
Definition: index.h:141
Form_pg_class rd_rel
Definition: rel.h:109
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:652
#define NAMEDATALEN
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define NoLock
Definition: lockdefs.h:34
#define RowExclusiveLock
Definition: lockdefs.h:38
void performDeletion(const ObjectAddress *object, DropBehavior behavior, int flags)
Definition: dependency.c:312
void RelationClearMissing(Relation rel)
Definition: heap.c:2061
static void swap_relation_files(Oid r1, Oid r2, bool target_is_pg_class, bool swap_toast_by_content, bool is_internal, TransactionId frozenXid, MultiXactId cutoffMulti, Oid *mapped_tables)
Definition: cluster.c:966
void CacheInvalidateCatalog(Oid catalogId)
Definition: inval.c:1254
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define REINDEX_REL_FORCE_INDEXES_UNLOGGED
Definition: index.h:143
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
void RelationMapRemoveMapping(Oid relationId)
Definition: relmapper.c:373
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
#define REINDEX_REL_CHECK_CONSTRAINTS
Definition: index.h:142
#define PROGRESS_CLUSTER_PHASE_FINAL_CLEANUP
Definition: progress.h:71
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:174
#define elog(elevel,...)
Definition: elog.h:214
int i
#define REINDEX_REL_FORCE_INDEXES_PERMANENT
Definition: index.h:144
#define PROGRESS_CLUSTER_PHASE_REBUILD_INDEX
Definition: progress.h:70
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
void RenameRelationInternal(Oid myrelid, const char *newrelname, bool is_internal, bool is_index)
Definition: tablecmds.c:3490
#define PROGRESS_CLUSTER_PHASE
Definition: progress.h:56
bool reindex_relation(Oid relid, int flags, int options)
Definition: index.c:3676
#define snprintf
Definition: port.h:215
Oid toast_get_valid_index(Oid toastoid, LOCKMODE lock)
#define PERFORM_DELETION_INTERNAL
Definition: dependency.h:134

◆ get_tables_to_cluster()

static List * get_tables_to_cluster ( MemoryContext  cluster_context)
static

Definition at line 1513 of file cluster.c.

References AccessShareLock, BoolGetDatum, BTEqualStrategyNumber, ForwardScanDirection, GETSTRUCT, GetUserId(), heap_getnext(), RelToCluster::indexOid, lappend(), MemoryContextSwitchTo(), NIL, palloc(), pg_class_ownercheck(), relation_close(), ScanKeyInit(), table_beginscan_catalog(), table_endscan(), table_open(), and RelToCluster::tableOid.

Referenced by cluster().

1514 {
1515  Relation indRelation;
1516  TableScanDesc scan;
1517  ScanKeyData entry;
1518  HeapTuple indexTuple;
1520  MemoryContext old_context;
1521  RelToCluster *rvtc;
1522  List *rvs = NIL;
1523 
1524  /*
1525  * Get all indexes that have indisclustered set and are owned by
1526  * appropriate user.
1527  */
1528  indRelation = table_open(IndexRelationId, AccessShareLock);
1529  ScanKeyInit(&entry,
1530  Anum_pg_index_indisclustered,
1531  BTEqualStrategyNumber, F_BOOLEQ,
1532  BoolGetDatum(true));
1533  scan = table_beginscan_catalog(indRelation, 1, &entry);
1534  while ((indexTuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1535  {
1536  index = (Form_pg_index) GETSTRUCT(indexTuple);
1537 
1538  if (!pg_class_ownercheck(index->indrelid, GetUserId()))
1539  continue;
1540 
1541  /*
1542  * We have to build the list in a different memory context so it will
1543  * survive the cross-transaction processing
1544  */
1545  old_context = MemoryContextSwitchTo(cluster_context);
1546 
1547  rvtc = (RelToCluster *) palloc(sizeof(RelToCluster));
1548  rvtc->tableOid = index->indrelid;
1549  rvtc->indexOid = index->indexrelid;
1550  rvs = lappend(rvs, rvtc);
1551 
1552  MemoryContextSwitchTo(old_context);
1553  }
1554  table_endscan(scan);
1555 
1556  relation_close(indRelation, AccessShareLock);
1557 
1558  return rvs;
1559 }
#define NIL
Definition: pg_list.h:65
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, struct ScanKeyData *key)
Definition: tableam.c:112
Oid GetUserId(void)
Definition: miscinit.c:476
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define AccessShareLock
Definition: lockdefs.h:36
Oid tableOid
Definition: cluster.c:65
Definition: type.h:89
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1286
List * lappend(List *list, void *datum)
Definition: list.c:321
FormData_pg_index * Form_pg_index
Definition: pg_index.h:68
#define BoolGetDatum(X)
Definition: postgres.h:402
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
bool pg_class_ownercheck(Oid class_oid, Oid roleid)
Definition: aclchk.c:4687
Oid indexOid
Definition: cluster.c:66
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:863
void * palloc(Size size)
Definition: mcxt.c:950
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Definition: pg_list.h:50
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ make_new_heap()

Oid make_new_heap ( Oid  OIDOldHeap,
Oid  NewTableSpace,
char  relpersistence,
LOCKMODE  lockmode 
)

Definition at line 605 of file cluster.c.

References Assert, CommandCounterIncrement(), elog, ERROR, heap_create_with_catalog(), HeapTupleIsValid, InvalidOid, LookupCreationNamespace(), NAMEDATALEN, NewHeapCreateToastTable(), NIL, NoLock, ObjectIdGetDatum, OidIsValid, ONCOMMIT_NOOP, RelationData::rd_rel, RelationGetDescr, RelationGetNamespace, RelationIsMapped, ReleaseSysCache(), RELOID, SearchSysCache1(), snprintf, SysCacheGetAttr(), table_close(), and table_open().

Referenced by ATRewriteTables(), ExecRefreshMatView(), and rebuild_relation().

607 {
608  TupleDesc OldHeapDesc;
609  char NewHeapName[NAMEDATALEN];
610  Oid OIDNewHeap;
611  Oid toastid;
612  Relation OldHeap;
613  HeapTuple tuple;
614  Datum reloptions;
615  bool isNull;
616  Oid namespaceid;
617 
618  OldHeap = table_open(OIDOldHeap, lockmode);
619  OldHeapDesc = RelationGetDescr(OldHeap);
620 
621  /*
622  * Note that the NewHeap will not receive any of the defaults or
623  * constraints associated with the OldHeap; we don't need 'em, and there's
624  * no reason to spend cycles inserting them into the catalogs only to
625  * delete them.
626  */
627 
628  /*
629  * But we do want to use reloptions of the old heap for new heap.
630  */
631  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(OIDOldHeap));
632  if (!HeapTupleIsValid(tuple))
633  elog(ERROR, "cache lookup failed for relation %u", OIDOldHeap);
634  reloptions = SysCacheGetAttr(RELOID, tuple, Anum_pg_class_reloptions,
635  &isNull);
636  if (isNull)
637  reloptions = (Datum) 0;
638 
639  if (relpersistence == RELPERSISTENCE_TEMP)
640  namespaceid = LookupCreationNamespace("pg_temp");
641  else
642  namespaceid = RelationGetNamespace(OldHeap);
643 
644  /*
645  * Create the new heap, using a temporary name in the same namespace as
646  * the existing table. NOTE: there is some risk of collision with user
647  * relnames. Working around this seems more trouble than it's worth; in
648  * particular, we can't create the new heap in a different namespace from
649  * the old, or we will have problems with the TEMP status of temp tables.
650  *
651  * Note: the new heap is not a shared relation, even if we are rebuilding
652  * a shared rel. However, we do make the new heap mapped if the source is
653  * mapped. This simplifies swap_relation_files, and is absolutely
654  * necessary for rebuilding pg_class, for reasons explained there.
655  */
656  snprintf(NewHeapName, sizeof(NewHeapName), "pg_temp_%u", OIDOldHeap);
657 
658  OIDNewHeap = heap_create_with_catalog(NewHeapName,
659  namespaceid,
660  NewTableSpace,
661  InvalidOid,
662  InvalidOid,
663  InvalidOid,
664  OldHeap->rd_rel->relowner,
665  OldHeap->rd_rel->relam,
666  OldHeapDesc,
667  NIL,
668  RELKIND_RELATION,
669  relpersistence,
670  false,
671  RelationIsMapped(OldHeap),
673  reloptions,
674  false,
675  true,
676  true,
677  OIDOldHeap,
678  NULL);
679  Assert(OIDNewHeap != InvalidOid);
680 
681  ReleaseSysCache(tuple);
682 
683  /*
684  * Advance command counter so that the newly-created relation's catalog
685  * tuples will be visible to table_open.
686  */
688 
689  /*
690  * If necessary, create a TOAST table for the new relation.
691  *
692  * If the relation doesn't have a TOAST table already, we can't need one
693  * for the new relation. The other way around is possible though: if some
694  * wide columns have been dropped, NewHeapCreateToastTable can decide that
695  * no TOAST table is needed for the new table.
696  *
697  * Note that NewHeapCreateToastTable ends with CommandCounterIncrement, so
698  * that the TOAST table will be visible for insertion.
699  */
700  toastid = OldHeap->rd_rel->reltoastrelid;
701  if (OidIsValid(toastid))
702  {
703  /* keep the existing toast table's reloptions, if any */
704  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(toastid));
705  if (!HeapTupleIsValid(tuple))
706  elog(ERROR, "cache lookup failed for relation %u", toastid);
707  reloptions = SysCacheGetAttr(RELOID, tuple, Anum_pg_class_reloptions,
708  &isNull);
709  if (isNull)
710  reloptions = (Datum) 0;
711 
712  NewHeapCreateToastTable(OIDNewHeap, reloptions, lockmode);
713 
714  ReleaseSysCache(tuple);
715  }
716 
717  table_close(OldHeap, NoLock);
718 
719  return OIDNewHeap;
720 }
#define NIL
Definition: pg_list.h:65
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define RelationGetDescr(relation)
Definition: rel.h:482
Oid LookupCreationNamespace(const char *nspname)
Definition: namespace.c:2935
Form_pg_class rd_rel
Definition: rel.h:109
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:652
#define NAMEDATALEN
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define NoLock
Definition: lockdefs.h:34
#define RelationIsMapped(relation)
Definition: rel.h:505
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
uintptr_t Datum
Definition: postgres.h:367
void CommandCounterIncrement(void)
Definition: xact.c:1021
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1377
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:746
Oid heap_create_with_catalog(const char *relname, Oid relnamespace, Oid reltablespace, Oid relid, Oid reltypeid, Oid reloftypeid, Oid ownerid, Oid accessmtd, TupleDesc tupdesc, List *cooked_constraints, char relkind, char relpersistence, bool shared_relation, bool mapped_relation, OnCommitAction oncommit, Datum reloptions, bool use_user_acl, bool allow_system_table_mods, bool is_internal, Oid relrewrite, ObjectAddress *typaddress)
Definition: heap.c:1131
#define elog(elevel,...)
Definition: elog.h:214
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define snprintf
Definition: port.h:215
#define RelationGetNamespace(relation)
Definition: rel.h:497
void NewHeapCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode)
Definition: toasting.c:63

◆ mark_index_clustered()

void mark_index_clustered ( Relation  rel,
Oid  indexOid,
bool  is_internal 
)

Definition at line 479 of file cluster.c.

References CatalogTupleUpdate(), elog, ereport, errcode(), errmsg(), ERROR, get_index_isclustered(), GETSTRUCT, heap_freetuple(), HeapTupleIsValid, INDEXRELID, InvalidOid, InvokeObjectPostAlterHookArg, lfirst_oid, ObjectIdGetDatum, OidIsValid, RelationData::rd_rel, RelationGetIndexList(), RowExclusiveLock, SearchSysCacheCopy1, HeapTupleData::t_self, table_close(), and table_open().

Referenced by ATExecClusterOn(), ATExecDropCluster(), and rebuild_relation().

480 {
481  HeapTuple indexTuple;
482  Form_pg_index indexForm;
483  Relation pg_index;
484  ListCell *index;
485 
486  /* Disallow applying to a partitioned table */
487  if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
488  ereport(ERROR,
489  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
490  errmsg("cannot mark index clustered in partitioned table")));
491 
492  /*
493  * If the index is already marked clustered, no need to do anything.
494  */
495  if (OidIsValid(indexOid))
496  {
497  if (get_index_isclustered(indexOid))
498  return;
499  }
500 
501  /*
502  * Check each index of the relation and set/clear the bit as needed.
503  */
504  pg_index = table_open(IndexRelationId, RowExclusiveLock);
505 
506  foreach(index, RelationGetIndexList(rel))
507  {
508  Oid thisIndexOid = lfirst_oid(index);
509 
510  indexTuple = SearchSysCacheCopy1(INDEXRELID,
511  ObjectIdGetDatum(thisIndexOid));
512  if (!HeapTupleIsValid(indexTuple))
513  elog(ERROR, "cache lookup failed for index %u", thisIndexOid);
514  indexForm = (Form_pg_index) GETSTRUCT(indexTuple);
515 
516  /*
517  * Unset the bit if set. We know it's wrong because we checked this
518  * earlier.
519  */
520  if (indexForm->indisclustered)
521  {
522  indexForm->indisclustered = false;
523  CatalogTupleUpdate(pg_index, &indexTuple->t_self, indexTuple);
524  }
525  else if (thisIndexOid == indexOid)
526  {
527  /* this was checked earlier, but let's be real sure */
528  if (!indexForm->indisvalid)
529  elog(ERROR, "cannot cluster on invalid index %u", indexOid);
530  indexForm->indisclustered = true;
531  CatalogTupleUpdate(pg_index, &indexTuple->t_self, indexTuple);
532  }
533 
534  InvokeObjectPostAlterHookArg(IndexRelationId, thisIndexOid, 0,
535  InvalidOid, is_internal);
536 
537  heap_freetuple(indexTuple);
538  }
539 
540  table_close(pg_index, RowExclusiveLock);
541 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
int errcode(int sqlerrcode)
Definition: elog.c:610
Form_pg_class rd_rel
Definition: rel.h:109
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:652
#define InvokeObjectPostAlterHookArg(classId, objectId, subId, auxiliaryId, is_internal)
Definition: objectaccess.h:178
Definition: type.h:89
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
FormData_pg_index * Form_pg_index
Definition: pg_index.h:68
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:144
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
bool get_index_isclustered(Oid index_oid)
Definition: lsyscache.c:3380
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4514
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:174
int errmsg(const char *fmt,...)
Definition: elog.c:821
#define elog(elevel,...)
Definition: elog.h:214
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define lfirst_oid(lc)
Definition: pg_list.h:171

◆ rebuild_relation()

static void rebuild_relation ( Relation  OldHeap,
Oid  indexOid,
bool  verbose 
)
static

Definition at line 552 of file cluster.c.

References AccessExclusiveLock, copy_table_data(), finish_heap_swap(), IsSystemRelation(), make_new_heap(), mark_index_clustered(), NoLock, OidIsValid, RelationData::rd_rel, RelationGetRelid, and table_close().

Referenced by cluster_rel().

553 {
554  Oid tableOid = RelationGetRelid(OldHeap);
555  Oid tableSpace = OldHeap->rd_rel->reltablespace;
556  Oid OIDNewHeap;
557  char relpersistence;
558  bool is_system_catalog;
559  bool swap_toast_by_content;
560  TransactionId frozenXid;
561  MultiXactId cutoffMulti;
562 
563  /* Mark the correct index as clustered */
564  if (OidIsValid(indexOid))
565  mark_index_clustered(OldHeap, indexOid, true);
566 
567  /* Remember info about rel before closing OldHeap */
568  relpersistence = OldHeap->rd_rel->relpersistence;
569  is_system_catalog = IsSystemRelation(OldHeap);
570 
571  /* Close relcache entry, but keep lock until transaction commit */
572  table_close(OldHeap, NoLock);
573 
574  /* Create the transient table that will receive the re-ordered data */
575  OIDNewHeap = make_new_heap(tableOid, tableSpace,
576  relpersistence,
578 
579  /* Copy the heap data into the new table in the desired order */
580  copy_table_data(OIDNewHeap, tableOid, indexOid, verbose,
581  &swap_toast_by_content, &frozenXid, &cutoffMulti);
582 
583  /*
584  * Swap the physical files of the target and transient tables, then
585  * rebuild the target's indexes and throw away the transient table.
586  */
587  finish_heap_swap(tableOid, OIDNewHeap, is_system_catalog,
588  swap_toast_by_content, false, true,
589  frozenXid, cutoffMulti,
590  relpersistence);
591 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
bool IsSystemRelation(Relation relation)
Definition: catalog.c:68
void finish_heap_swap(Oid OIDOldHeap, Oid OIDNewHeap, bool is_system_catalog, bool swap_toast_by_content, bool check_constraints, bool is_internal, TransactionId frozenXid, MultiXactId cutoffMulti, char newrelpersistence)
Definition: cluster.c:1324
uint32 TransactionId
Definition: c.h:521
Form_pg_class rd_rel
Definition: rel.h:109
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:652
#define NoLock
Definition: lockdefs.h:34
static int verbose
void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal)
Definition: cluster.c:479
TransactionId MultiXactId
Definition: c.h:531
static void copy_table_data(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex, bool verbose, bool *pSwapToastByContent, TransactionId *pFreezeXid, MultiXactId *pCutoffMulti)
Definition: cluster.c:731
#define AccessExclusiveLock
Definition: lockdefs.h:45
#define RelationGetRelid(relation)
Definition: rel.h:456
Oid make_new_heap(Oid OIDOldHeap, Oid NewTableSpace, char relpersistence, LOCKMODE lockmode)
Definition: cluster.c:605

◆ swap_relation_files()

static void swap_relation_files ( Oid  r1,
Oid  r2,
bool  target_is_pg_class,
bool  swap_toast_by_content,
bool  is_internal,
TransactionId  frozenXid,
MultiXactId  cutoffMulti,
Oid mapped_tables 
)
static

Definition at line 966 of file cluster.c.

References AccessExclusiveLock, Assert, CacheInvalidateRelcacheByTuple(), CatalogCloseIndexes(), CatalogOpenIndexes(), CatalogTupleUpdateWithInfo(), ObjectAddress::classId, deleteDependencyRecordsFor(), DEPENDENCY_INTERNAL, elog, ERROR, GETSTRUCT, heap_freetuple(), HeapTupleIsValid, InvalidMultiXactId, InvalidOid, InvalidTransactionId, InvokeObjectPostAlterHookArg, IsSystemClass(), NameStr, NoLock, ObjectAddress::objectId, ObjectIdGetDatum, ObjectAddress::objectSubId, OidIsValid, RelationData::rd_createSubid, RelationData::rd_firstRelfilenodeSubid, RelationData::rd_newRelfilenodeSubid, recordDependencyOn(), relation_close(), relation_open(), RelationAssumeNewRelfilenode(), RelationCloseSmgrByOid(), RelationMapOidToFilenode(), RelationMapUpdateMap(), RELOID, RowExclusiveLock, SearchSysCacheCopy1, HeapTupleData::t_self, table_close(), table_open(), toast_get_valid_index(), TransactionIdIsNormal, and TransactionIdIsValid.

Referenced by finish_heap_swap().

972 {
973  Relation relRelation;
974  HeapTuple reltup1,
975  reltup2;
976  Form_pg_class relform1,
977  relform2;
978  Oid relfilenode1,
979  relfilenode2;
980  Oid swaptemp;
981  char swptmpchr;
982 
983  /* We need writable copies of both pg_class tuples. */
984  relRelation = table_open(RelationRelationId, RowExclusiveLock);
985 
987  if (!HeapTupleIsValid(reltup1))
988  elog(ERROR, "cache lookup failed for relation %u", r1);
989  relform1 = (Form_pg_class) GETSTRUCT(reltup1);
990 
992  if (!HeapTupleIsValid(reltup2))
993  elog(ERROR, "cache lookup failed for relation %u", r2);
994  relform2 = (Form_pg_class) GETSTRUCT(reltup2);
995 
996  relfilenode1 = relform1->relfilenode;
997  relfilenode2 = relform2->relfilenode;
998 
999  if (OidIsValid(relfilenode1) && OidIsValid(relfilenode2))
1000  {
1001  /*
1002  * Normal non-mapped relations: swap relfilenodes, reltablespaces,
1003  * relpersistence
1004  */
1005  Assert(!target_is_pg_class);
1006 
1007  swaptemp = relform1->relfilenode;
1008  relform1->relfilenode = relform2->relfilenode;
1009  relform2->relfilenode = swaptemp;
1010 
1011  swaptemp = relform1->reltablespace;
1012  relform1->reltablespace = relform2->reltablespace;
1013  relform2->reltablespace = swaptemp;
1014 
1015  swptmpchr = relform1->relpersistence;
1016  relform1->relpersistence = relform2->relpersistence;
1017  relform2->relpersistence = swptmpchr;
1018 
1019  /* Also swap toast links, if we're swapping by links */
1020  if (!swap_toast_by_content)
1021  {
1022  swaptemp = relform1->reltoastrelid;
1023  relform1->reltoastrelid = relform2->reltoastrelid;
1024  relform2->reltoastrelid = swaptemp;
1025  }
1026  }
1027  else
1028  {
1029  /*
1030  * Mapped-relation case. Here we have to swap the relation mappings
1031  * instead of modifying the pg_class columns. Both must be mapped.
1032  */
1033  if (OidIsValid(relfilenode1) || OidIsValid(relfilenode2))
1034  elog(ERROR, "cannot swap mapped relation \"%s\" with non-mapped relation",
1035  NameStr(relform1->relname));
1036 
1037  /*
1038  * We can't change the tablespace nor persistence of a mapped rel, and
1039  * we can't handle toast link swapping for one either, because we must
1040  * not apply any critical changes to its pg_class row. These cases
1041  * should be prevented by upstream permissions tests, so these checks
1042  * are non-user-facing emergency backstop.
1043  */
1044  if (relform1->reltablespace != relform2->reltablespace)
1045  elog(ERROR, "cannot change tablespace of mapped relation \"%s\"",
1046  NameStr(relform1->relname));
1047  if (relform1->relpersistence != relform2->relpersistence)
1048  elog(ERROR, "cannot change persistence of mapped relation \"%s\"",
1049  NameStr(relform1->relname));
1050  if (!swap_toast_by_content &&
1051  (relform1->reltoastrelid || relform2->reltoastrelid))
1052  elog(ERROR, "cannot swap toast by links for mapped relation \"%s\"",
1053  NameStr(relform1->relname));
1054 
1055  /*
1056  * Fetch the mappings --- shouldn't fail, but be paranoid
1057  */
1058  relfilenode1 = RelationMapOidToFilenode(r1, relform1->relisshared);
1059  if (!OidIsValid(relfilenode1))
1060  elog(ERROR, "could not find relation mapping for relation \"%s\", OID %u",
1061  NameStr(relform1->relname), r1);
1062  relfilenode2 = RelationMapOidToFilenode(r2, relform2->relisshared);
1063  if (!OidIsValid(relfilenode2))
1064  elog(ERROR, "could not find relation mapping for relation \"%s\", OID %u",
1065  NameStr(relform2->relname), r2);
1066 
1067  /*
1068  * Send replacement mappings to relmapper. Note these won't actually
1069  * take effect until CommandCounterIncrement.
1070  */
1071  RelationMapUpdateMap(r1, relfilenode2, relform1->relisshared, false);
1072  RelationMapUpdateMap(r2, relfilenode1, relform2->relisshared, false);
1073 
1074  /* Pass OIDs of mapped r2 tables back to caller */
1075  *mapped_tables++ = r2;
1076  }
1077 
1078  /*
1079  * Recognize that rel1's relfilenode (swapped from rel2) is new in this
1080  * subtransaction. The rel2 storage (swapped from rel1) may or may not be
1081  * new.
1082  */
1083  {
1084  Relation rel1,
1085  rel2;
1086 
1087  rel1 = relation_open(r1, NoLock);
1088  rel2 = relation_open(r2, NoLock);
1089  rel2->rd_createSubid = rel1->rd_createSubid;
1093  relation_close(rel1, NoLock);
1094  relation_close(rel2, NoLock);
1095  }
1096 
1097  /*
1098  * In the case of a shared catalog, these next few steps will only affect
1099  * our own database's pg_class row; but that's okay, because they are all
1100  * noncritical updates. That's also an important fact for the case of a
1101  * mapped catalog, because it's possible that we'll commit the map change
1102  * and then fail to commit the pg_class update.
1103  */
1104 
1105  /* set rel1's frozen Xid and minimum MultiXid */
1106  if (relform1->relkind != RELKIND_INDEX)
1107  {
1108  Assert(!TransactionIdIsValid(frozenXid) ||
1109  TransactionIdIsNormal(frozenXid));
1110  relform1->relfrozenxid = frozenXid;
1111  relform1->relminmxid = cutoffMulti;
1112  }
1113 
1114  /* swap size statistics too, since new rel has freshly-updated stats */
1115  {
1116  int32 swap_pages;
1117  float4 swap_tuples;
1118  int32 swap_allvisible;
1119 
1120  swap_pages = relform1->relpages;
1121  relform1->relpages = relform2->relpages;
1122  relform2->relpages = swap_pages;
1123 
1124  swap_tuples = relform1->reltuples;
1125  relform1->reltuples = relform2->reltuples;
1126  relform2->reltuples = swap_tuples;
1127 
1128  swap_allvisible = relform1->relallvisible;
1129  relform1->relallvisible = relform2->relallvisible;
1130  relform2->relallvisible = swap_allvisible;
1131  }
1132 
1133  /*
1134  * Update the tuples in pg_class --- unless the target relation of the
1135  * swap is pg_class itself. In that case, there is zero point in making
1136  * changes because we'd be updating the old data that we're about to throw
1137  * away. Because the real work being done here for a mapped relation is
1138  * just to change the relation map settings, it's all right to not update
1139  * the pg_class rows in this case. The most important changes will instead
1140  * performed later, in finish_heap_swap() itself.
1141  */
1142  if (!target_is_pg_class)
1143  {
1144  CatalogIndexState indstate;
1145 
1146  indstate = CatalogOpenIndexes(relRelation);
1147  CatalogTupleUpdateWithInfo(relRelation, &reltup1->t_self, reltup1,
1148  indstate);
1149  CatalogTupleUpdateWithInfo(relRelation, &reltup2->t_self, reltup2,
1150  indstate);
1151  CatalogCloseIndexes(indstate);
1152  }
1153  else
1154  {
1155  /* no update ... but we do still need relcache inval */
1158  }
1159 
1160  /*
1161  * Post alter hook for modified relations. The change to r2 is always
1162  * internal, but r1 depends on the invocation context.
1163  */
1164  InvokeObjectPostAlterHookArg(RelationRelationId, r1, 0,
1165  InvalidOid, is_internal);
1166  InvokeObjectPostAlterHookArg(RelationRelationId, r2, 0,
1167  InvalidOid, true);
1168 
1169  /*
1170  * If we have toast tables associated with the relations being swapped,
1171  * deal with them too.
1172  */
1173  if (relform1->reltoastrelid || relform2->reltoastrelid)
1174  {
1175  if (swap_toast_by_content)
1176  {
1177  if (relform1->reltoastrelid && relform2->reltoastrelid)
1178  {
1179  /* Recursively swap the contents of the toast tables */
1180  swap_relation_files(relform1->reltoastrelid,
1181  relform2->reltoastrelid,
1182  target_is_pg_class,
1183  swap_toast_by_content,
1184  is_internal,
1185  frozenXid,
1186  cutoffMulti,
1187  mapped_tables);
1188  }
1189  else
1190  {
1191  /* caller messed up */
1192  elog(ERROR, "cannot swap toast files by content when there's only one");
1193  }
1194  }
1195  else
1196  {
1197  /*
1198  * We swapped the ownership links, so we need to change dependency
1199  * data to match.
1200  *
1201  * NOTE: it is possible that only one table has a toast table.
1202  *
1203  * NOTE: at present, a TOAST table's only dependency is the one on
1204  * its owning table. If more are ever created, we'd need to use
1205  * something more selective than deleteDependencyRecordsFor() to
1206  * get rid of just the link we want.
1207  */
1208  ObjectAddress baseobject,
1209  toastobject;
1210  long count;
1211 
1212  /*
1213  * We disallow this case for system catalogs, to avoid the
1214  * possibility that the catalog we're rebuilding is one of the
1215  * ones the dependency changes would change. It's too late to be
1216  * making any data changes to the target catalog.
1217  */
1218  if (IsSystemClass(r1, relform1))
1219  elog(ERROR, "cannot swap toast files by links for system catalogs");
1220 
1221  /* Delete old dependencies */
1222  if (relform1->reltoastrelid)
1223  {
1224  count = deleteDependencyRecordsFor(RelationRelationId,
1225  relform1->reltoastrelid,
1226  false);
1227  if (count != 1)
1228  elog(ERROR, "expected one dependency record for TOAST table, found %ld",
1229  count);
1230  }
1231  if (relform2->reltoastrelid)
1232  {
1233  count = deleteDependencyRecordsFor(RelationRelationId,
1234  relform2->reltoastrelid,
1235  false);
1236  if (count != 1)
1237  elog(ERROR, "expected one dependency record for TOAST table, found %ld",
1238  count);
1239  }
1240 
1241  /* Register new dependencies */
1242  baseobject.classId = RelationRelationId;
1243  baseobject.objectSubId = 0;
1244  toastobject.classId = RelationRelationId;
1245  toastobject.objectSubId = 0;
1246 
1247  if (relform1->reltoastrelid)
1248  {
1249  baseobject.objectId = r1;
1250  toastobject.objectId = relform1->reltoastrelid;
1251  recordDependencyOn(&toastobject, &baseobject,
1253  }
1254 
1255  if (relform2->reltoastrelid)
1256  {
1257  baseobject.objectId = r2;
1258  toastobject.objectId = relform2->reltoastrelid;
1259  recordDependencyOn(&toastobject, &baseobject,
1261  }
1262  }
1263  }
1264 
1265  /*
1266  * If we're swapping two toast tables by content, do the same for their
1267  * valid index. The swap can actually be safely done only if the relations
1268  * have indexes.
1269  */
1270  if (swap_toast_by_content &&
1271  relform1->relkind == RELKIND_TOASTVALUE &&
1272  relform2->relkind == RELKIND_TOASTVALUE)
1273  {
1274  Oid toastIndex1,
1275  toastIndex2;
1276 
1277  /* Get valid index for each relation */
1278  toastIndex1 = toast_get_valid_index(r1,
1280  toastIndex2 = toast_get_valid_index(r2,
1282 
1283  swap_relation_files(toastIndex1,
1284  toastIndex2,
1285  target_is_pg_class,
1286  swap_toast_by_content,
1287  is_internal,
1290  mapped_tables);
1291  }
1292 
1293  /* Clean up. */
1294  heap_freetuple(reltup1);
1295  heap_freetuple(reltup2);
1296 
1297  table_close(relRelation, RowExclusiveLock);
1298 
1299  /*
1300  * Close both relcache entries' smgr links. We need this kluge because
1301  * both links will be invalidated during upcoming CommandCounterIncrement.
1302  * Whichever of the rels is the second to be cleared will have a dangling
1303  * reference to the other's smgr entry. Rather than trying to avoid this
1304  * by ordering operations just so, it's easiest to close the links first.
1305  * (Fortunately, since one of the entries is local in our transaction,
1306  * it's sufficient to clear out our own relcache this way; the problem
1307  * cannot arise for other backends when they see our update on the
1308  * non-transient relation.)
1309  *
1310  * Caution: the placement of this step interacts with the decision to
1311  * handle toast rels by recursion. When we are trying to rebuild pg_class
1312  * itself, the smgr close on pg_class must happen after all accesses in
1313  * this function.
1314  */
1317 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
long deleteDependencyRecordsFor(Oid classId, Oid objectId, bool skipExtensionDeps)
Definition: pg_depend.c:232
SubTransactionId rd_newRelfilenodeSubid
Definition: rel.h:103
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:43
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:652
#define InvokeObjectPostAlterHookArg(classId, objectId, subId, auxiliaryId, is_internal)
Definition: objectaccess.h:178
signed int int32
Definition: c.h:363
bool IsSystemClass(Oid relid, Form_pg_class reltuple)
Definition: catalog.c:80
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48
ItemPointerData t_self
Definition: htup.h:65
#define NoLock
Definition: lockdefs.h:34
#define RowExclusiveLock
Definition: lockdefs.h:38
#define InvalidTransactionId
Definition: transam.h:31
void CatalogTupleUpdateWithInfo(Relation heapRel, ItemPointer otid, HeapTuple tup, CatalogIndexState indstate)
Definition: indexing.c:324
float float4
Definition: c.h:498
SubTransactionId rd_createSubid
Definition: rel.h:102
void RelationAssumeNewRelfilenode(Relation relation)
Definition: relcache.c:3738
SubTransactionId rd_firstRelfilenodeSubid
Definition: rel.h:105
Oid RelationMapOidToFilenode(Oid relationId, bool shared)
Definition: relmapper.c:159
#define InvalidMultiXactId
Definition: multixact.h:24
static void swap_relation_files(Oid r1, Oid r2, bool target_is_pg_class, bool swap_toast_by_content, bool is_internal, TransactionId frozenXid, MultiXactId cutoffMulti, Oid *mapped_tables)
Definition: cluster.c:966
#define InvalidOid
Definition: postgres_ext.h:36
void RelationCloseSmgrByOid(Oid relationId)
Definition: relcache.c:2934
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
#define Assert(condition)
Definition: c.h:746
CatalogIndexState CatalogOpenIndexes(Relation heapRel)
Definition: indexing.c:43
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:174
#define AccessExclusiveLock
Definition: lockdefs.h:45
#define elog(elevel,...)
Definition: elog.h:214
#define NameStr(name)
Definition: c.h:623
void CatalogCloseIndexes(CatalogIndexState indstate)
Definition: indexing.c:61
void CacheInvalidateRelcacheByTuple(HeapTuple classTuple)
Definition: inval.c:1314
#define TransactionIdIsValid(xid)
Definition: transam.h:41
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
void RelationMapUpdateMap(Oid relationId, Oid fileNode, bool shared, bool immediate)
Definition: relmapper.c:261
Oid toast_get_valid_index(Oid toastoid, LOCKMODE lock)