PostgreSQL Source Code  git master
cluster.h File Reference
#include "nodes/parsenodes.h"
#include "parser/parse_node.h"
#include "storage/lock.h"
#include "utils/relcache.h"
Include dependency graph for cluster.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ClusterParams
 

Macros

#define CLUOPT_RECHECK   0x01 /* recheck relation state */
 
#define CLUOPT_VERBOSE   0x02 /* print progress info */
 

Typedefs

typedef struct ClusterParams ClusterParams
 

Functions

void cluster (ParseState *pstate, ClusterStmt *stmt, bool isTopLevel)
 
void cluster_rel (Oid tableOid, Oid indexOid, ClusterParams *params)
 
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, Oid NewAccessMethod, char relpersistence, LOCKMODE lockmode)
 
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 minMulti, char newrelpersistence)
 

Macro Definition Documentation

◆ CLUOPT_RECHECK

#define CLUOPT_RECHECK   0x01 /* recheck relation state */

Definition at line 23 of file cluster.h.

Referenced by cluster(), and cluster_rel().

◆ CLUOPT_VERBOSE

#define CLUOPT_VERBOSE   0x02 /* print progress info */

Definition at line 24 of file cluster.h.

Referenced by cluster(), cluster_rel(), and vacuum_rel().

Typedef Documentation

◆ ClusterParams

typedef struct ClusterParams ClusterParams

Function Documentation

◆ check_index_is_clusterable()

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

Definition at line 443 of file cluster.c.

References IndexAmRoutine::amclusterable, ereport, errcode(), 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().

444 {
445  Relation OldIndex;
446 
447  OldIndex = index_open(indexOid, lockmode);
448 
449  /*
450  * Check that index is in fact an index on the given relation
451  */
452  if (OldIndex->rd_index == NULL ||
453  OldIndex->rd_index->indrelid != RelationGetRelid(OldHeap))
454  ereport(ERROR,
455  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
456  errmsg("\"%s\" is not an index for table \"%s\"",
457  RelationGetRelationName(OldIndex),
458  RelationGetRelationName(OldHeap))));
459 
460  /* Index AM must allow clustering */
461  if (!OldIndex->rd_indam->amclusterable)
462  ereport(ERROR,
463  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
464  errmsg("cannot cluster on index \"%s\" because access method does not support clustering",
465  RelationGetRelationName(OldIndex))));
466 
467  /*
468  * Disallow clustering on incomplete indexes (those that might not index
469  * every row of the relation). We could relax this by making a separate
470  * seqscan pass over the table to copy the missing rows, but that seems
471  * expensive and tedious.
472  */
473  if (!heap_attisnull(OldIndex->rd_indextuple, Anum_pg_index_indpred, NULL))
474  ereport(ERROR,
475  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
476  errmsg("cannot cluster on partial index \"%s\"",
477  RelationGetRelationName(OldIndex))));
478 
479  /*
480  * Disallow if index is left over from a failed CREATE INDEX CONCURRENTLY;
481  * it might well not contain entries for every heap row, or might not even
482  * be internally consistent. (But note that we don't check indcheckxmin;
483  * the worst consequence of following broken HOT chains would be that we
484  * might put recently-dead tuples out-of-order in the new table, and there
485  * is little harm in that.)
486  */
487  if (!OldIndex->rd_index->indisvalid)
488  ereport(ERROR,
489  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
490  errmsg("cannot cluster on invalid index \"%s\"",
491  RelationGetRelationName(OldIndex))));
492 
493  /* Drop relcache refcnt on OldIndex, but keep lock */
494  index_close(OldIndex, NoLock);
495 }
struct IndexAmRoutine * rd_indam
Definition: rel.h:201
int errcode(int sqlerrcode)
Definition: elog.c:698
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:359
struct HeapTupleData * rd_indextuple
Definition: rel.h:189
Form_pg_index rd_index
Definition: rel.h:187
#define ERROR
Definition: elog.h:46
#define NoLock
Definition: lockdefs.h:34
#define RelationGetRelationName(relation)
Definition: rel.h:511
bool amclusterable
Definition: amapi.h:238
#define ereport(elevel,...)
Definition: elog.h:157
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:158
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define RelationGetRelid(relation)
Definition: rel.h:477
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:132

◆ cluster()

void cluster ( ParseState pstate,
ClusterStmt stmt,
bool  isTopLevel 
)

Definition at line 103 of file cluster.c.

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

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

104 {
105  ListCell *lc;
106  ClusterParams params = {0};
107  bool verbose = false;
108 
109  /* Parse option list */
110  foreach(lc, stmt->params)
111  {
112  DefElem *opt = (DefElem *) lfirst(lc);
113 
114  if (strcmp(opt->defname, "verbose") == 0)
115  verbose = defGetBoolean(opt);
116  else
117  ereport(ERROR,
118  (errcode(ERRCODE_SYNTAX_ERROR),
119  errmsg("unrecognized CLUSTER option \"%s\"",
120  opt->defname),
121  parser_errposition(pstate, opt->location)));
122  }
123 
124  params.options = (verbose ? CLUOPT_VERBOSE : 0);
125 
126  if (stmt->relation != NULL)
127  {
128  /* This is the single-relation case. */
129  Oid tableOid,
130  indexOid = InvalidOid;
131  Relation rel;
132 
133  /* Find, lock, and check permissions on the table */
134  tableOid = RangeVarGetRelidExtended(stmt->relation,
136  0,
138  rel = table_open(tableOid, NoLock);
139 
140  /*
141  * Reject clustering a remote temp table ... their local buffer
142  * manager is not going to cope.
143  */
144  if (RELATION_IS_OTHER_TEMP(rel))
145  ereport(ERROR,
146  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
147  errmsg("cannot cluster temporary tables of other sessions")));
148 
149  /*
150  * Reject clustering a partitioned table.
151  */
152  if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
153  ereport(ERROR,
154  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
155  errmsg("cannot cluster a partitioned table")));
156 
157  if (stmt->indexname == NULL)
158  {
159  ListCell *index;
160 
161  /* We need to find the index that has indisclustered set. */
162  foreach(index, RelationGetIndexList(rel))
163  {
164  indexOid = lfirst_oid(index);
165  if (get_index_isclustered(indexOid))
166  break;
167  indexOid = InvalidOid;
168  }
169 
170  if (!OidIsValid(indexOid))
171  ereport(ERROR,
172  (errcode(ERRCODE_UNDEFINED_OBJECT),
173  errmsg("there is no previously clustered index for table \"%s\"",
174  stmt->relation->relname)));
175  }
176  else
177  {
178  /*
179  * The index is expected to be in the same namespace as the
180  * relation.
181  */
182  indexOid = get_relname_relid(stmt->indexname,
183  rel->rd_rel->relnamespace);
184  if (!OidIsValid(indexOid))
185  ereport(ERROR,
186  (errcode(ERRCODE_UNDEFINED_OBJECT),
187  errmsg("index \"%s\" for table \"%s\" does not exist",
188  stmt->indexname, stmt->relation->relname)));
189  }
190 
191  /* close relation, keep lock till commit */
192  table_close(rel, NoLock);
193 
194  /* Do the job. */
195  cluster_rel(tableOid, indexOid, &params);
196  }
197  else
198  {
199  /*
200  * This is the "multi relation" case. We need to cluster all tables
201  * that have some index with indisclustered set.
202  */
203  MemoryContext cluster_context;
204  List *rvs;
205  ListCell *rv;
206 
207  /*
208  * We cannot run this form of CLUSTER inside a user transaction block;
209  * we'd be holding locks way too long.
210  */
211  PreventInTransactionBlock(isTopLevel, "CLUSTER");
212 
213  /*
214  * Create special memory context for cross-transaction storage.
215  *
216  * Since it is a child of PortalContext, it will go away even in case
217  * of error.
218  */
219  cluster_context = AllocSetContextCreate(PortalContext,
220  "Cluster",
222 
223  /*
224  * Build the list of relations to cluster. Note that this lives in
225  * cluster_context.
226  */
227  rvs = get_tables_to_cluster(cluster_context);
228 
229  /* Commit to get out of starting transaction */
232 
233  /* Ok, now that we've got them all, cluster them one by one */
234  foreach(rv, rvs)
235  {
236  RelToCluster *rvtc = (RelToCluster *) lfirst(rv);
237  ClusterParams cluster_params = params;
238 
239  /* Start a new transaction for each relation. */
241  /* functions in indexes may want a snapshot set */
243  /* Do the job. */
244  cluster_params.options |= CLUOPT_RECHECK;
245  cluster_rel(rvtc->tableOid, rvtc->indexOid,
246  &cluster_params);
249  }
250 
251  /* Start a new transaction for the cleanup work. */
253 
254  /* Clean up working storage */
255  MemoryContextDelete(cluster_context);
256  }
257 }
List * params
Definition: parsenodes.h:3344
void RangeVarCallbackOwnsTable(const RangeVar *relation, Oid relId, Oid oldRelId, void *arg)
Definition: tablecmds.c:16486
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
#define AllocSetContextCreate
Definition: memutils.h:173
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void CommitTransactionCommand(void)
Definition: xact.c:2953
int errcode(int sqlerrcode)
Definition: elog.c:698
#define CLUOPT_VERBOSE
Definition: cluster.h:24
void PopActiveSnapshot(void)
Definition: snapmgr.c:774
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:710
Oid tableOid
Definition: cluster.c:66
MemoryContext PortalContext
Definition: mcxt.c:57
Definition: type.h:89
char * relname
Definition: primnodes.h:68
bool defGetBoolean(DefElem *def)
Definition: define.c:106
char * indexname
Definition: parsenodes.h:3343
#define ERROR
Definition: elog.h:46
Oid get_relname_relid(const char *relname, Oid relnamespace)
Definition: lsyscache.c:1856
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:195
#define NoLock
Definition: lockdefs.h:34
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:680
int location
Definition: parsenodes.h:761
void PreventInTransactionBlock(bool isTopLevel, const char *stmtType)
Definition: xact.c:3393
void cluster_rel(Oid tableOid, Oid indexOid, ClusterParams *params)
Definition: cluster.c:277
Oid RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, uint32 flags, RangeVarGetRelidCallback callback, void *callback_arg)
Definition: namespace.c:237
static List * get_tables_to_cluster(MemoryContext cluster_context)
Definition: cluster.c:1560
static int verbose
bits32 options
Definition: cluster.h:29
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:157
#define lfirst(lc)
Definition: pg_list.h:169
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:631
void StartTransactionCommand(void)
Definition: xact.c:2852
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
bool get_index_isclustered(Oid index_oid)
Definition: lsyscache.c:3555
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4573
Oid indexOid
Definition: cluster.c:67
#define CLUOPT_RECHECK
Definition: cluster.h:23
#define AccessExclusiveLock
Definition: lockdefs.h:45
int errmsg(const char *fmt,...)
Definition: elog.c:909
char * defname
Definition: parsenodes.h:758
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
RangeVar * relation
Definition: parsenodes.h:3342
Definition: pg_list.h:50
#define lfirst_oid(lc)
Definition: pg_list.h:171

◆ cluster_rel()

void cluster_rel ( Oid  tableOid,
Oid  indexOid,
ClusterParams params 
)

Definition at line 277 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, ClusterParams::options, 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().

278 {
279  Relation OldHeap;
280  bool verbose = ((params->options & CLUOPT_VERBOSE) != 0);
281  bool recheck = ((params->options & CLUOPT_RECHECK) != 0);
282 
283  /* Check for user-requested abort. */
285 
287  if (OidIsValid(indexOid))
290  else
293 
294  /*
295  * We grab exclusive access to the target rel and index for the duration
296  * of the transaction. (This is redundant for the single-transaction
297  * case, since cluster() already did it.) The index lock is taken inside
298  * check_index_is_clusterable.
299  */
300  OldHeap = try_relation_open(tableOid, AccessExclusiveLock);
301 
302  /* If the table has gone away, we can skip processing it */
303  if (!OldHeap)
304  {
306  return;
307  }
308 
309  /*
310  * Since we may open a new transaction for each relation, we have to check
311  * that the relation still is what we think it is.
312  *
313  * If this is a single-transaction CLUSTER, we can skip these tests. We
314  * *must* skip the one on indisclustered since it would reject an attempt
315  * to cluster a not-previously-clustered index.
316  */
317  if (recheck)
318  {
319  /* Check that the user still owns the relation */
320  if (!pg_class_ownercheck(tableOid, GetUserId()))
321  {
324  return;
325  }
326 
327  /*
328  * Silently skip a temp table for a remote session. Only doing this
329  * check in the "recheck" case is appropriate (which currently means
330  * somebody is executing a database-wide CLUSTER), because there is
331  * another check in cluster() which will stop any attempt to cluster
332  * remote temp tables by name. There is another check in cluster_rel
333  * which is redundant, but we leave it for extra safety.
334  */
335  if (RELATION_IS_OTHER_TEMP(OldHeap))
336  {
339  return;
340  }
341 
342  if (OidIsValid(indexOid))
343  {
344  /*
345  * Check that the index still exists
346  */
348  {
351  return;
352  }
353 
354  /*
355  * Check that the index is still the one with indisclustered set.
356  */
357  if (!get_index_isclustered(indexOid))
358  {
361  return;
362  }
363  }
364  }
365 
366  /*
367  * We allow VACUUM FULL, but not CLUSTER, on shared catalogs. CLUSTER
368  * would work in most respects, but the index would only get marked as
369  * indisclustered in the current database, leading to unexpected behavior
370  * if CLUSTER were later invoked in another database.
371  */
372  if (OidIsValid(indexOid) && OldHeap->rd_rel->relisshared)
373  ereport(ERROR,
374  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
375  errmsg("cannot cluster a shared catalog")));
376 
377  /*
378  * Don't process temp tables of other backends ... their local buffer
379  * manager is not going to cope.
380  */
381  if (RELATION_IS_OTHER_TEMP(OldHeap))
382  {
383  if (OidIsValid(indexOid))
384  ereport(ERROR,
385  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
386  errmsg("cannot cluster temporary tables of other sessions")));
387  else
388  ereport(ERROR,
389  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
390  errmsg("cannot vacuum temporary tables of other sessions")));
391  }
392 
393  /*
394  * Also check for active uses of the relation in the current transaction,
395  * including open scans and pending AFTER trigger events.
396  */
397  CheckTableNotInUse(OldHeap, OidIsValid(indexOid) ? "CLUSTER" : "VACUUM");
398 
399  /* Check heap and index are valid to cluster on */
400  if (OidIsValid(indexOid))
401  check_index_is_clusterable(OldHeap, indexOid, recheck, AccessExclusiveLock);
402 
403  /*
404  * Quietly ignore the request if this is a materialized view which has not
405  * been populated from its query. No harm is done because there is no data
406  * to deal with, and we don't want to throw an error if this is part of a
407  * multi-relation request -- for example, CLUSTER was run on the entire
408  * database.
409  */
410  if (OldHeap->rd_rel->relkind == RELKIND_MATVIEW &&
411  !RelationIsPopulated(OldHeap))
412  {
415  return;
416  }
417 
418  /*
419  * All predicate locks on the tuples or pages are about to be made
420  * invalid, because we move tuples around. Promote them to relation
421  * locks. Predicate locks on indexes will be promoted when they are
422  * reindexed.
423  */
425 
426  /* rebuild_relation does all the dirty work */
427  rebuild_relation(OldHeap, indexOid, verbose);
428 
429  /* NB: rebuild_relation does table_close() on OldHeap */
430 
432 }
#define RelationIsPopulated(relation)
Definition: rel.h:650
Oid GetUserId(void)
Definition: miscinit.c:495
int errcode(int sqlerrcode)
Definition: elog.c:698
#define CLUOPT_VERBOSE
Definition: cluster.h:24
Form_pg_class rd_rel
Definition: rel.h:109
#define OidIsValid(objectId)
Definition: c.h:710
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:89
void pgstat_progress_end_command(void)
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:184
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
static void rebuild_relation(Relation OldHeap, Oid indexOid, bool verbose)
Definition: cluster.c:576
#define PROGRESS_CLUSTER_COMMAND_VACUUM_FULL
Definition: progress.h:75
void pgstat_progress_start_command(ProgressCommandType cmdtype, Oid relid)
#define PROGRESS_CLUSTER_COMMAND_CLUSTER
Definition: progress.h:74
void CheckTableNotInUse(Relation rel, const char *stmt)
Definition: tablecmds.c:3943
#define PROGRESS_CLUSTER_COMMAND
Definition: progress.h:55
void TransferPredicateLocksToHeapRelation(Relation relation)
Definition: predicate.c:3146
static int verbose
bits32 options
Definition: cluster.h:29
#define ereport(elevel,...)
Definition: elog.h:157
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
void pgstat_progress_update_param(int index, int64 val)
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:631
bool pg_class_ownercheck(Oid class_oid, Oid roleid)
Definition: aclchk.c:4818
bool get_index_isclustered(Oid index_oid)
Definition: lsyscache.c:3555
#define CLUOPT_RECHECK
Definition: cluster.h:23
#define AccessExclusiveLock
Definition: lockdefs.h:45
int errmsg(const char *fmt,...)
Definition: elog.c:909
void check_index_is_clusterable(Relation OldHeap, Oid indexOid, bool recheck, LOCKMODE lockmode)
Definition: cluster.c:443
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120

◆ 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  minMulti,
char  newrelpersistence 
)

Definition at line 1362 of file cluster.c.

References CacheInvalidateCatalog(), CatalogTupleUpdate(), CommandCounterIncrement(), 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(), ResetRelRewrite(), 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().

1370 {
1371  ObjectAddress object;
1372  Oid mapped_tables[4];
1373  int reindex_flags;
1374  ReindexParams reindex_params = {0};
1375  int i;
1376 
1377  /* Report that we are now swapping relation files */
1380 
1381  /* Zero out possible results from swapped_relation_files */
1382  memset(mapped_tables, 0, sizeof(mapped_tables));
1383 
1384  /*
1385  * Swap the contents of the heap relations (including any toast tables).
1386  * Also set old heap's relfrozenxid to frozenXid.
1387  */
1388  swap_relation_files(OIDOldHeap, OIDNewHeap,
1389  (OIDOldHeap == RelationRelationId),
1390  swap_toast_by_content, is_internal,
1391  frozenXid, cutoffMulti, mapped_tables);
1392 
1393  /*
1394  * If it's a system catalog, queue a sinval message to flush all catcaches
1395  * on the catalog when we reach CommandCounterIncrement.
1396  */
1397  if (is_system_catalog)
1398  CacheInvalidateCatalog(OIDOldHeap);
1399 
1400  /*
1401  * Rebuild each index on the relation (but not the toast table, which is
1402  * all-new at this point). It is important to do this before the DROP
1403  * step because if we are processing a system catalog that will be used
1404  * during DROP, we want to have its indexes available. There is no
1405  * advantage to the other order anyway because this is all transactional,
1406  * so no chance to reclaim disk space before commit. We do not need a
1407  * final CommandCounterIncrement() because reindex_relation does it.
1408  *
1409  * Note: because index_build is called via reindex_relation, it will never
1410  * set indcheckxmin true for the indexes. This is OK even though in some
1411  * sense we are building new indexes rather than rebuilding existing ones,
1412  * because the new heap won't contain any HOT chains at all, let alone
1413  * broken ones, so it can't be necessary to set indcheckxmin.
1414  */
1415  reindex_flags = REINDEX_REL_SUPPRESS_INDEX_USE;
1416  if (check_constraints)
1417  reindex_flags |= REINDEX_REL_CHECK_CONSTRAINTS;
1418 
1419  /*
1420  * Ensure that the indexes have the same persistence as the parent
1421  * relation.
1422  */
1423  if (newrelpersistence == RELPERSISTENCE_UNLOGGED)
1424  reindex_flags |= REINDEX_REL_FORCE_INDEXES_UNLOGGED;
1425  else if (newrelpersistence == RELPERSISTENCE_PERMANENT)
1426  reindex_flags |= REINDEX_REL_FORCE_INDEXES_PERMANENT;
1427 
1428  /* Report that we are now reindexing relations */
1431 
1432  reindex_relation(OIDOldHeap, reindex_flags, &reindex_params);
1433 
1434  /* Report that we are now doing clean up */
1437 
1438  /*
1439  * If the relation being rebuilt is pg_class, swap_relation_files()
1440  * couldn't update pg_class's own pg_class entry (check comments in
1441  * swap_relation_files()), thus relfrozenxid was not updated. That's
1442  * annoying because a potential reason for doing a VACUUM FULL is a
1443  * imminent or actual anti-wraparound shutdown. So, now that we can
1444  * access the new relation using its indices, update relfrozenxid.
1445  * pg_class doesn't have a toast relation, so we don't need to update the
1446  * corresponding toast relation. Not that there's little point moving all
1447  * relfrozenxid updates here since swap_relation_files() needs to write to
1448  * pg_class for non-mapped relations anyway.
1449  */
1450  if (OIDOldHeap == RelationRelationId)
1451  {
1452  Relation relRelation;
1453  HeapTuple reltup;
1454  Form_pg_class relform;
1455 
1456  relRelation = table_open(RelationRelationId, RowExclusiveLock);
1457 
1458  reltup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(OIDOldHeap));
1459  if (!HeapTupleIsValid(reltup))
1460  elog(ERROR, "cache lookup failed for relation %u", OIDOldHeap);
1461  relform = (Form_pg_class) GETSTRUCT(reltup);
1462 
1463  relform->relfrozenxid = frozenXid;
1464  relform->relminmxid = cutoffMulti;
1465 
1466  CatalogTupleUpdate(relRelation, &reltup->t_self, reltup);
1467 
1468  table_close(relRelation, RowExclusiveLock);
1469  }
1470 
1471  /* Destroy new heap with old filenode */
1472  object.classId = RelationRelationId;
1473  object.objectId = OIDNewHeap;
1474  object.objectSubId = 0;
1475 
1476  /*
1477  * The new relation is local to our transaction and we know nothing
1478  * depends on it, so DROP_RESTRICT should be OK.
1479  */
1481 
1482  /* performDeletion does CommandCounterIncrement at end */
1483 
1484  /*
1485  * Now we must remove any relation mapping entries that we set up for the
1486  * transient table, as well as its toast table and toast index if any. If
1487  * we fail to do this before commit, the relmapper will complain about new
1488  * permanent map entries being added post-bootstrap.
1489  */
1490  for (i = 0; OidIsValid(mapped_tables[i]); i++)
1491  RelationMapRemoveMapping(mapped_tables[i]);
1492 
1493  /*
1494  * At this point, everything is kosher except that, if we did toast swap
1495  * by links, the toast table's name corresponds to the transient table.
1496  * The name is irrelevant to the backend because it's referenced by OID,
1497  * but users looking at the catalogs could be confused. Rename it to
1498  * prevent this problem.
1499  *
1500  * Note no lock required on the relation, because we already hold an
1501  * exclusive lock on it.
1502  */
1503  if (!swap_toast_by_content)
1504  {
1505  Relation newrel;
1506 
1507  newrel = table_open(OIDOldHeap, NoLock);
1508  if (OidIsValid(newrel->rd_rel->reltoastrelid))
1509  {
1510  Oid toastidx;
1511  char NewToastName[NAMEDATALEN];
1512 
1513  /* Get the associated valid index to be renamed */
1514  toastidx = toast_get_valid_index(newrel->rd_rel->reltoastrelid,
1515  NoLock);
1516 
1517  /* rename the toast table ... */
1518  snprintf(NewToastName, NAMEDATALEN, "pg_toast_%u",
1519  OIDOldHeap);
1520  RenameRelationInternal(newrel->rd_rel->reltoastrelid,
1521  NewToastName, true, false);
1522 
1523  /* ... and its valid index too. */
1524  snprintf(NewToastName, NAMEDATALEN, "pg_toast_%u_index",
1525  OIDOldHeap);
1526 
1527  RenameRelationInternal(toastidx,
1528  NewToastName, true, true);
1529 
1530  /*
1531  * Reset the relrewrite for the toast. The command-counter
1532  * increment is required here as we are about to update
1533  * the tuple that is updated as part of RenameRelationInternal.
1534  */
1536  ResetRelRewrite(newrel->rd_rel->reltoastrelid);
1537  }
1538  relation_close(newrel, NoLock);
1539  }
1540 
1541  /* if it's not a catalog table, clear any missing attribute settings */
1542  if (!is_system_catalog)
1543  {
1544  Relation newrel;
1545 
1546  newrel = table_open(OIDOldHeap, NoLock);
1547  RelationClearMissing(newrel);
1548  relation_close(newrel, NoLock);
1549  }
1550 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define PROGRESS_CLUSTER_PHASE_SWAP_REL_FILES
Definition: progress.h:69
#define REINDEX_REL_SUPPRESS_INDEX_USE
Definition: index.h:156
Form_pg_class rd_rel
Definition: rel.h:109
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
#define NAMEDATALEN
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
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:313
void RelationClearMissing(Relation rel)
Definition: heap.c:2079
void CommandCounterIncrement(void)
Definition: xact.c:1022
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:997
void CacheInvalidateCatalog(Oid catalogId)
Definition: inval.c:1318
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define REINDEX_REL_FORCE_INDEXES_UNLOGGED
Definition: index.h:158
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
void RelationMapRemoveMapping(Oid relationId)
Definition: relmapper.c:373
void pgstat_progress_update_param(int index, int64 val)
void ResetRelRewrite(Oid myrelid)
Definition: tablecmds.c:3890
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
#define REINDEX_REL_CHECK_CONSTRAINTS
Definition: index.h:157
#define PROGRESS_CLUSTER_PHASE_FINAL_CLEANUP
Definition: progress.h:71
bool reindex_relation(Oid relid, int flags, ReindexParams *params)
Definition: index.c:3795
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:175
#define elog(elevel,...)
Definition: elog.h:232
int i
#define REINDEX_REL_FORCE_INDEXES_PERMANENT
Definition: index.h:159
#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:3800
#define PROGRESS_CLUSTER_PHASE
Definition: progress.h:56
#define snprintf
Definition: port.h:217
Oid toast_get_valid_index(Oid toastoid, LOCKMODE lock)
#define PERFORM_DELETION_INTERNAL
Definition: dependency.h:133

◆ make_new_heap()

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

Definition at line 631 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().

633 {
634  TupleDesc OldHeapDesc;
635  char NewHeapName[NAMEDATALEN];
636  Oid OIDNewHeap;
637  Oid toastid;
638  Relation OldHeap;
639  HeapTuple tuple;
640  Datum reloptions;
641  bool isNull;
642  Oid namespaceid;
643 
644  OldHeap = table_open(OIDOldHeap, lockmode);
645  OldHeapDesc = RelationGetDescr(OldHeap);
646 
647  /*
648  * Note that the NewHeap will not receive any of the defaults or
649  * constraints associated with the OldHeap; we don't need 'em, and there's
650  * no reason to spend cycles inserting them into the catalogs only to
651  * delete them.
652  */
653 
654  /*
655  * But we do want to use reloptions of the old heap for new heap.
656  */
657  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(OIDOldHeap));
658  if (!HeapTupleIsValid(tuple))
659  elog(ERROR, "cache lookup failed for relation %u", OIDOldHeap);
660  reloptions = SysCacheGetAttr(RELOID, tuple, Anum_pg_class_reloptions,
661  &isNull);
662  if (isNull)
663  reloptions = (Datum) 0;
664 
665  if (relpersistence == RELPERSISTENCE_TEMP)
666  namespaceid = LookupCreationNamespace("pg_temp");
667  else
668  namespaceid = RelationGetNamespace(OldHeap);
669 
670  /*
671  * Create the new heap, using a temporary name in the same namespace as
672  * the existing table. NOTE: there is some risk of collision with user
673  * relnames. Working around this seems more trouble than it's worth; in
674  * particular, we can't create the new heap in a different namespace from
675  * the old, or we will have problems with the TEMP status of temp tables.
676  *
677  * Note: the new heap is not a shared relation, even if we are rebuilding
678  * a shared rel. However, we do make the new heap mapped if the source is
679  * mapped. This simplifies swap_relation_files, and is absolutely
680  * necessary for rebuilding pg_class, for reasons explained there.
681  */
682  snprintf(NewHeapName, sizeof(NewHeapName), "pg_temp_%u", OIDOldHeap);
683 
684  OIDNewHeap = heap_create_with_catalog(NewHeapName,
685  namespaceid,
686  NewTableSpace,
687  InvalidOid,
688  InvalidOid,
689  InvalidOid,
690  OldHeap->rd_rel->relowner,
691  NewAccessMethod,
692  OldHeapDesc,
693  NIL,
694  RELKIND_RELATION,
695  relpersistence,
696  false,
697  RelationIsMapped(OldHeap),
699  reloptions,
700  false,
701  true,
702  true,
703  OIDOldHeap,
704  NULL);
705  Assert(OIDNewHeap != InvalidOid);
706 
707  ReleaseSysCache(tuple);
708 
709  /*
710  * Advance command counter so that the newly-created relation's catalog
711  * tuples will be visible to table_open.
712  */
714 
715  /*
716  * If necessary, create a TOAST table for the new relation.
717  *
718  * If the relation doesn't have a TOAST table already, we can't need one
719  * for the new relation. The other way around is possible though: if some
720  * wide columns have been dropped, NewHeapCreateToastTable can decide that
721  * no TOAST table is needed for the new table.
722  *
723  * Note that NewHeapCreateToastTable ends with CommandCounterIncrement, so
724  * that the TOAST table will be visible for insertion.
725  */
726  toastid = OldHeap->rd_rel->reltoastrelid;
727  if (OidIsValid(toastid))
728  {
729  /* keep the existing toast table's reloptions, if any */
730  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(toastid));
731  if (!HeapTupleIsValid(tuple))
732  elog(ERROR, "cache lookup failed for relation %u", toastid);
733  reloptions = SysCacheGetAttr(RELOID, tuple, Anum_pg_class_reloptions,
734  &isNull);
735  if (isNull)
736  reloptions = (Datum) 0;
737 
738  NewHeapCreateToastTable(OIDNewHeap, reloptions, lockmode, toastid);
739 
740  ReleaseSysCache(tuple);
741  }
742 
743  table_close(OldHeap, NoLock);
744 
745  return OIDNewHeap;
746 }
#define NIL
Definition: pg_list.h:65
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define RelationGetDescr(relation)
Definition: rel.h:503
Oid LookupCreationNamespace(const char *nspname)
Definition: namespace.c:2981
Form_pg_class rd_rel
Definition: rel.h:109
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
#define NAMEDATALEN
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define NoLock
Definition: lockdefs.h:34
#define RelationIsMapped(relation)
Definition: rel.h:526
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
void NewHeapCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode, Oid OIDOldToast)
Definition: toasting.c:66
uintptr_t Datum
Definition: postgres.h:411
void CommandCounterIncrement(void)
Definition: xact.c:1022
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:804
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:1143
#define elog(elevel,...)
Definition: elog.h:232
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define snprintf
Definition: port.h:217
#define RelationGetNamespace(relation)
Definition: rel.h:518

◆ mark_index_clustered()

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

Definition at line 503 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().

504 {
505  HeapTuple indexTuple;
506  Form_pg_index indexForm;
507  Relation pg_index;
508  ListCell *index;
509 
510  /* Disallow applying to a partitioned table */
511  if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
512  ereport(ERROR,
513  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
514  errmsg("cannot mark index clustered in partitioned table")));
515 
516  /*
517  * If the index is already marked clustered, no need to do anything.
518  */
519  if (OidIsValid(indexOid))
520  {
521  if (get_index_isclustered(indexOid))
522  return;
523  }
524 
525  /*
526  * Check each index of the relation and set/clear the bit as needed.
527  */
528  pg_index = table_open(IndexRelationId, RowExclusiveLock);
529 
530  foreach(index, RelationGetIndexList(rel))
531  {
532  Oid thisIndexOid = lfirst_oid(index);
533 
534  indexTuple = SearchSysCacheCopy1(INDEXRELID,
535  ObjectIdGetDatum(thisIndexOid));
536  if (!HeapTupleIsValid(indexTuple))
537  elog(ERROR, "cache lookup failed for index %u", thisIndexOid);
538  indexForm = (Form_pg_index) GETSTRUCT(indexTuple);
539 
540  /*
541  * Unset the bit if set. We know it's wrong because we checked this
542  * earlier.
543  */
544  if (indexForm->indisclustered)
545  {
546  indexForm->indisclustered = false;
547  CatalogTupleUpdate(pg_index, &indexTuple->t_self, indexTuple);
548  }
549  else if (thisIndexOid == indexOid)
550  {
551  /* this was checked earlier, but let's be real sure */
552  if (!indexForm->indisvalid)
553  elog(ERROR, "cannot cluster on invalid index %u", indexOid);
554  indexForm->indisclustered = true;
555  CatalogTupleUpdate(pg_index, &indexTuple->t_self, indexTuple);
556  }
557 
558  InvokeObjectPostAlterHookArg(IndexRelationId, thisIndexOid, 0,
559  InvalidOid, is_internal);
560 
561  heap_freetuple(indexTuple);
562  }
563 
564  table_close(pg_index, RowExclusiveLock);
565 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
int errcode(int sqlerrcode)
Definition: elog.c:698
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:710
#define InvokeObjectPostAlterHookArg(classId, objectId, subId, auxiliaryId, is_internal)
Definition: objectaccess.h:178
Definition: type.h:89
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
FormData_pg_index * Form_pg_index
Definition: pg_index.h:69
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:157
#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:3555
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4573
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:175
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define lfirst_oid(lc)
Definition: pg_list.h:171