PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
cluster.c File Reference
#include "postgres.h"
#include "access/amapi.h"
#include "access/multixact.h"
#include "access/relscan.h"
#include "access/rewriteheap.h"
#include "access/transam.h"
#include "access/tuptoaster.h"
#include "access/xact.h"
#include "access/xlog.h"
#include "catalog/pg_am.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/toasting.h"
#include "commands/cluster.h"
#include "commands/tablecmds.h"
#include "commands/vacuum.h"
#include "miscadmin.h"
#include "optimizer/planner.h"
#include "storage/bufmgr.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/smgr.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/tqual.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_heap_data (Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex, bool verbose, bool *pSwapToastByContent, TransactionId *pFreezeXid, MultiXactId *pCutoffMulti)
 
static Listget_tables_to_cluster (MemoryContext cluster_context)
 
static void reform_and_rewrite_tuple (HeapTuple tuple, TupleDesc oldTupDesc, TupleDesc newTupDesc, Datum *values, bool *isnull, bool newRelHasOids, RewriteState rwstate)
 
void cluster (ClusterStmt *stmt, bool isTopLevel)
 
void cluster_rel (Oid tableOid, Oid indexOid, bool recheck, bool verbose)
 
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

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

Definition at line 418 of file cluster.c.

References IndexAmRoutine::amclusterable, Anum_pg_index_indpred, ereport, errcode(), errmsg(), ERROR, heap_attisnull(), index_close(), index_open(), IndexIsValid, NoLock, NULL, RelationData::rd_amroutine, RelationData::rd_index, RelationData::rd_indextuple, RelationGetRelationName, and RelationGetRelid.

Referenced by ATExecClusterOn(), and cluster_rel().

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

Definition at line 106 of file cluster.c.

References AccessExclusiveLock, ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), cluster_rel(), CommitTransactionCommand(), elog, ereport, errcode(), errmsg(), ERROR, get_relname_relid(), get_tables_to_cluster(), GETSTRUCT, GetTransactionSnapshot(), heap_close, heap_open(), HeapTupleIsValid, ClusterStmt::indexname, RelToCluster::indexOid, INDEXRELID, InvalidOid, lfirst, lfirst_oid, MemoryContextDelete(), NoLock, NULL, ObjectIdGetDatum, OidIsValid, PopActiveSnapshot(), PortalContext, PreventTransactionChain(), PushActiveSnapshot(), RangeVarCallbackOwnsTable(), RangeVarGetRelidExtended(), RelationData::rd_rel, ClusterStmt::relation, RELATION_IS_OTHER_TEMP, RelationGetIndexList(), ReleaseSysCache(), RangeVar::relname, SearchSysCache1, StartTransactionCommand(), RelToCluster::tableOid, and ClusterStmt::verbose.

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

107 {
108  if (stmt->relation != NULL)
109  {
110  /* This is the single-relation case. */
111  Oid tableOid,
112  indexOid = InvalidOid;
113  Relation rel;
114 
115  /* Find, lock, and check permissions on the table */
116  tableOid = RangeVarGetRelidExtended(stmt->relation,
118  false, false,
120  rel = heap_open(tableOid, NoLock);
121 
122  /*
123  * Reject clustering a remote temp table ... their local buffer
124  * manager is not going to cope.
125  */
126  if (RELATION_IS_OTHER_TEMP(rel))
127  ereport(ERROR,
128  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
129  errmsg("cannot cluster temporary tables of other sessions")));
130 
131  if (stmt->indexname == NULL)
132  {
133  ListCell *index;
134 
135  /* We need to find the index that has indisclustered set. */
136  foreach(index, RelationGetIndexList(rel))
137  {
138  HeapTuple idxtuple;
139  Form_pg_index indexForm;
140 
141  indexOid = lfirst_oid(index);
142  idxtuple = SearchSysCache1(INDEXRELID,
143  ObjectIdGetDatum(indexOid));
144  if (!HeapTupleIsValid(idxtuple))
145  elog(ERROR, "cache lookup failed for index %u", indexOid);
146  indexForm = (Form_pg_index) GETSTRUCT(idxtuple);
147  if (indexForm->indisclustered)
148  {
149  ReleaseSysCache(idxtuple);
150  break;
151  }
152  ReleaseSysCache(idxtuple);
153  indexOid = InvalidOid;
154  }
155 
156  if (!OidIsValid(indexOid))
157  ereport(ERROR,
158  (errcode(ERRCODE_UNDEFINED_OBJECT),
159  errmsg("there is no previously clustered index for table \"%s\"",
160  stmt->relation->relname)));
161  }
162  else
163  {
164  /*
165  * The index is expected to be in the same namespace as the
166  * relation.
167  */
168  indexOid = get_relname_relid(stmt->indexname,
169  rel->rd_rel->relnamespace);
170  if (!OidIsValid(indexOid))
171  ereport(ERROR,
172  (errcode(ERRCODE_UNDEFINED_OBJECT),
173  errmsg("index \"%s\" for table \"%s\" does not exist",
174  stmt->indexname, stmt->relation->relname)));
175  }
176 
177  /* close relation, keep lock till commit */
178  heap_close(rel, NoLock);
179 
180  /* Do the job. */
181  cluster_rel(tableOid, indexOid, false, stmt->verbose);
182  }
183  else
184  {
185  /*
186  * This is the "multi relation" case. We need to cluster all tables
187  * that have some index with indisclustered set.
188  */
189  MemoryContext cluster_context;
190  List *rvs;
191  ListCell *rv;
192 
193  /*
194  * We cannot run this form of CLUSTER inside a user transaction block;
195  * we'd be holding locks way too long.
196  */
197  PreventTransactionChain(isTopLevel, "CLUSTER");
198 
199  /*
200  * Create special memory context for cross-transaction storage.
201  *
202  * Since it is a child of PortalContext, it will go away even in case
203  * of error.
204  */
205  cluster_context = AllocSetContextCreate(PortalContext,
206  "Cluster",
208 
209  /*
210  * Build the list of relations to cluster. Note that this lives in
211  * cluster_context.
212  */
213  rvs = get_tables_to_cluster(cluster_context);
214 
215  /* Commit to get out of starting transaction */
218 
219  /* Ok, now that we've got them all, cluster them one by one */
220  foreach(rv, rvs)
221  {
222  RelToCluster *rvtc = (RelToCluster *) lfirst(rv);
223 
224  /* Start a new transaction for each relation. */
226  /* functions in indexes may want a snapshot set */
228  /* Do the job. */
229  cluster_rel(rvtc->tableOid, rvtc->indexOid, true, stmt->verbose);
232  }
233 
234  /* Start a new transaction for the cleanup work. */
236 
237  /* Clean up working storage */
238  MemoryContextDelete(cluster_context);
239  }
240 }
void RangeVarCallbackOwnsTable(const RangeVar *relation, Oid relId, Oid oldRelId, void *arg)
Definition: tablecmds.c:12585
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
Oid RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, bool missing_ok, bool nowait, RangeVarGetRelidCallback callback, void *callback_arg)
Definition: namespace.c:217
void CommitTransactionCommand(void)
Definition: xact.c:2745
int errcode(int sqlerrcode)
Definition: elog.c:575
void PopActiveSnapshot(void)
Definition: snapmgr.c:807
#define heap_close(r, l)
Definition: heapam.h:97
Form_pg_class rd_rel
Definition: rel.h:113
unsigned int Oid
Definition: postgres_ext.h:31
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:300
#define OidIsValid(objectId)
Definition: c.h:534
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:149
Oid tableOid
Definition: cluster.c:65
MemoryContext PortalContext
Definition: mcxt.c:52
Definition: type.h:90
char * relname
Definition: primnodes.h:67
char * indexname
Definition: parsenodes.h:2957
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
void cluster_rel(Oid tableOid, Oid indexOid, bool recheck, bool verbose)
Definition: cluster.c:260
Oid get_relname_relid(const char *relname, Oid relnamespace)
Definition: lsyscache.c:1651
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:145
#define NoLock
Definition: lockdefs.h:34
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:728
static List * get_tables_to_cluster(MemoryContext cluster_context)
Definition: cluster.c:1634
#define ereport(elevel, rest)
Definition: elog.h:122
FormData_pg_index * Form_pg_index
Definition: pg_index.h:67
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:440
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1083
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:530
void StartTransactionCommand(void)
Definition: xact.c:2675
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4336
Oid indexOid
Definition: cluster.c:66
#define AccessExclusiveLock
Definition: lockdefs.h:46
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
RangeVar * relation
Definition: parsenodes.h:2956
Definition: pg_list.h:45
#define lfirst_oid(lc)
Definition: pg_list.h:108
void PreventTransactionChain(bool isTopLevel, const char *stmtType)
Definition: xact.c:3152
void cluster_rel ( Oid  tableOid,
Oid  indexOid,
bool  recheck,
bool  verbose 
)

Definition at line 260 of file cluster.c.

References AccessExclusiveLock, CHECK_FOR_INTERRUPTS, check_index_is_clusterable(), CheckTableNotInUse(), ereport, errcode(), errmsg(), ERROR, GETSTRUCT, GetUserId(), HeapTupleIsValid, INDEXRELID, ObjectIdGetDatum, OidIsValid, pg_class_ownercheck(), RelationData::rd_rel, rebuild_relation(), relation_close(), RELATION_IS_OTHER_TEMP, RelationIsPopulated, ReleaseSysCache(), RELKIND_MATVIEW, RELOID, SearchSysCache1, SearchSysCacheExists1, TransferPredicateLocksToHeapRelation(), and try_relation_open().

Referenced by cluster(), and vacuum_rel().

261 {
262  Relation OldHeap;
263 
264  /* Check for user-requested abort. */
266 
267  /*
268  * We grab exclusive access to the target rel and index for the duration
269  * of the transaction. (This is redundant for the single-transaction
270  * case, since cluster() already did it.) The index lock is taken inside
271  * check_index_is_clusterable.
272  */
273  OldHeap = try_relation_open(tableOid, AccessExclusiveLock);
274 
275  /* If the table has gone away, we can skip processing it */
276  if (!OldHeap)
277  return;
278 
279  /*
280  * Since we may open a new transaction for each relation, we have to check
281  * that the relation still is what we think it is.
282  *
283  * If this is a single-transaction CLUSTER, we can skip these tests. We
284  * *must* skip the one on indisclustered since it would reject an attempt
285  * to cluster a not-previously-clustered index.
286  */
287  if (recheck)
288  {
289  HeapTuple tuple;
290  Form_pg_index indexForm;
291 
292  /* Check that the user still owns the relation */
293  if (!pg_class_ownercheck(tableOid, GetUserId()))
294  {
296  return;
297  }
298 
299  /*
300  * Silently skip a temp table for a remote session. Only doing this
301  * check in the "recheck" case is appropriate (which currently means
302  * somebody is executing a database-wide CLUSTER), because there is
303  * another check in cluster() which will stop any attempt to cluster
304  * remote temp tables by name. There is another check in cluster_rel
305  * which is redundant, but we leave it for extra safety.
306  */
307  if (RELATION_IS_OTHER_TEMP(OldHeap))
308  {
310  return;
311  }
312 
313  if (OidIsValid(indexOid))
314  {
315  /*
316  * Check that the index still exists
317  */
319  {
321  return;
322  }
323 
324  /*
325  * Check that the index is still the one with indisclustered set.
326  */
327  tuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexOid));
328  if (!HeapTupleIsValid(tuple)) /* probably can't happen */
329  {
331  return;
332  }
333  indexForm = (Form_pg_index) GETSTRUCT(tuple);
334  if (!indexForm->indisclustered)
335  {
336  ReleaseSysCache(tuple);
338  return;
339  }
340  ReleaseSysCache(tuple);
341  }
342  }
343 
344  /*
345  * We allow VACUUM FULL, but not CLUSTER, on shared catalogs. CLUSTER
346  * would work in most respects, but the index would only get marked as
347  * indisclustered in the current database, leading to unexpected behavior
348  * if CLUSTER were later invoked in another database.
349  */
350  if (OidIsValid(indexOid) && OldHeap->rd_rel->relisshared)
351  ereport(ERROR,
352  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
353  errmsg("cannot cluster a shared catalog")));
354 
355  /*
356  * Don't process temp tables of other backends ... their local buffer
357  * manager is not going to cope.
358  */
359  if (RELATION_IS_OTHER_TEMP(OldHeap))
360  {
361  if (OidIsValid(indexOid))
362  ereport(ERROR,
363  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
364  errmsg("cannot cluster temporary tables of other sessions")));
365  else
366  ereport(ERROR,
367  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
368  errmsg("cannot vacuum temporary tables of other sessions")));
369  }
370 
371  /*
372  * Also check for active uses of the relation in the current transaction,
373  * including open scans and pending AFTER trigger events.
374  */
375  CheckTableNotInUse(OldHeap, OidIsValid(indexOid) ? "CLUSTER" : "VACUUM");
376 
377  /* Check heap and index are valid to cluster on */
378  if (OidIsValid(indexOid))
379  check_index_is_clusterable(OldHeap, indexOid, recheck, AccessExclusiveLock);
380 
381  /*
382  * Quietly ignore the request if this is a materialized view which has not
383  * been populated from its query. No harm is done because there is no data
384  * to deal with, and we don't want to throw an error if this is part of a
385  * multi-relation request -- for example, CLUSTER was run on the entire
386  * database.
387  */
388  if (OldHeap->rd_rel->relkind == RELKIND_MATVIEW &&
389  !RelationIsPopulated(OldHeap))
390  {
392  return;
393  }
394 
395  /*
396  * All predicate locks on the tuples or pages are about to be made
397  * invalid, because we move tuples around. Promote them to relation
398  * locks. Predicate locks on indexes will be promoted when they are
399  * reindexed.
400  */
402 
403  /* rebuild_relation does all the dirty work */
404  rebuild_relation(OldHeap, indexOid, verbose);
405 
406  /* NB: rebuild_relation does heap_close() on OldHeap */
407 }
#define RelationIsPopulated(relation)
Definition: rel.h:549
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1150
Oid GetUserId(void)
Definition: miscinit.c:282
#define RELKIND_MATVIEW
Definition: pg_class.h:167
int errcode(int sqlerrcode)
Definition: elog.c:575
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1263
Form_pg_class rd_rel
Definition: rel.h:113
#define OidIsValid(objectId)
Definition: c.h:534
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:149
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:167
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
static void rebuild_relation(Relation OldHeap, Oid indexOid, bool verbose)
Definition: cluster.c:555
void CheckTableNotInUse(Relation rel, const char *stmt)
Definition: tablecmds.c:2950
#define ereport(elevel, rest)
Definition: elog.h:122
void TransferPredicateLocksToHeapRelation(Relation relation)
Definition: predicate.c:3007
static int verbose
Definition: pg_basebackup.c:86
FormData_pg_index * Form_pg_index
Definition: pg_index.h:67
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1083
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:530
bool pg_class_ownercheck(Oid class_oid, Oid roleid)
Definition: aclchk.c:4521
#define AccessExclusiveLock
Definition: lockdefs.h:46
int errmsg(const char *fmt,...)
Definition: elog.c:797
void check_index_is_clusterable(Relation OldHeap, Oid indexOid, bool recheck, LOCKMODE lockmode)
Definition: cluster.c:418
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
static void copy_heap_data ( Oid  OIDNewHeap,
Oid  OIDOldHeap,
Oid  OIDOldIndex,
bool  verbose,
bool pSwapToastByContent,
TransactionId pFreezeXid,
MultiXactId pCutoffMulti 
)
static

Definition at line 732 of file cluster.c.

References AccessExclusiveLock, Assert, begin_heap_rewrite(), BTREE_AM_OID, buf, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, CHECK_FOR_INTERRUPTS, DEBUG2, elevel, elog, end_heap_rewrite(), ereport, errdetail(), errmsg(), ERROR, ForwardScanDirection, get_namespace_name(), heap_beginscan(), heap_close, heap_endscan(), heap_getnext(), heap_open(), HEAPTUPLE_DEAD, HEAPTUPLE_DELETE_IN_PROGRESS, HEAPTUPLE_INSERT_IN_PROGRESS, HEAPTUPLE_LIVE, HEAPTUPLE_RECENTLY_DEAD, HeapTupleHeaderGetUpdateXid, HeapTupleHeaderGetXmin, HeapTupleSatisfiesVacuum(), index_beginscan(), index_close(), index_endscan(), index_getnext(), index_open(), index_rescan(), INFO, InvalidBlockNumber, InvalidOid, IsSystemRelation(), LockBuffer(), LockRelationOid(), maintenance_work_mem, MultiXactCutoff, MultiXactIdPrecedes(), tupleDesc::natts, NoLock, NULL, OidIsValid, OldestXmin, palloc(), pfree(), pg_rusage_init(), pg_rusage_show(), plan_cluster_use_sort(), RelationData::rd_rel, RelationData::rd_toastoid, reform_and_rewrite_tuple(), RelationGetDescr, RelationGetNamespace, RelationGetNumberOfBlocks, RelationGetRelationName, RelationGetTargetBlock, RelationNeedsWAL, rewrite_heap_dead_tuple(), HeapScanDescData::rs_cbuf, SnapshotAny, HeapTupleData::t_data, TransactionIdIsCurrentTransactionId(), TransactionIdPrecedes(), tuplesort_begin_cluster(), tuplesort_end(), tuplesort_getheaptuple(), tuplesort_performsort(), tuplesort_putheaptuple(), vacuum_set_xid_limits(), values, WARNING, XLogIsNeeded, IndexScanDescData::xs_cbuf, and IndexScanDescData::xs_recheck.

Referenced by rebuild_relation().

735 {
736  Relation NewHeap,
737  OldHeap,
738  OldIndex;
739  TupleDesc oldTupDesc;
740  TupleDesc newTupDesc;
741  int natts;
742  Datum *values;
743  bool *isnull;
744  IndexScanDesc indexScan;
745  HeapScanDesc heapScan;
746  bool use_wal;
747  bool is_system_catalog;
749  TransactionId FreezeXid;
751  RewriteState rwstate;
752  bool use_sort;
753  Tuplesortstate *tuplesort;
754  double num_tuples = 0,
755  tups_vacuumed = 0,
756  tups_recently_dead = 0;
757  int elevel = verbose ? INFO : DEBUG2;
758  PGRUsage ru0;
759 
760  pg_rusage_init(&ru0);
761 
762  /*
763  * Open the relations we need.
764  */
765  NewHeap = heap_open(OIDNewHeap, AccessExclusiveLock);
766  OldHeap = heap_open(OIDOldHeap, AccessExclusiveLock);
767  if (OidIsValid(OIDOldIndex))
768  OldIndex = index_open(OIDOldIndex, AccessExclusiveLock);
769  else
770  OldIndex = NULL;
771 
772  /*
773  * Their tuple descriptors should be exactly alike, but here we only need
774  * assume that they have the same number of columns.
775  */
776  oldTupDesc = RelationGetDescr(OldHeap);
777  newTupDesc = RelationGetDescr(NewHeap);
778  Assert(newTupDesc->natts == oldTupDesc->natts);
779 
780  /* Preallocate values/isnull arrays */
781  natts = newTupDesc->natts;
782  values = (Datum *) palloc(natts * sizeof(Datum));
783  isnull = (bool *) palloc(natts * sizeof(bool));
784 
785  /*
786  * If the OldHeap has a toast table, get lock on the toast table to keep
787  * it from being vacuumed. This is needed because autovacuum processes
788  * toast tables independently of their main tables, with no lock on the
789  * latter. If an autovacuum were to start on the toast table after we
790  * compute our OldestXmin below, it would use a later OldestXmin, and then
791  * possibly remove as DEAD toast tuples belonging to main tuples we think
792  * are only RECENTLY_DEAD. Then we'd fail while trying to copy those
793  * tuples.
794  *
795  * We don't need to open the toast relation here, just lock it. The lock
796  * will be held till end of transaction.
797  */
798  if (OldHeap->rd_rel->reltoastrelid)
799  LockRelationOid(OldHeap->rd_rel->reltoastrelid, AccessExclusiveLock);
800 
801  /*
802  * We need to log the copied data in WAL iff WAL archiving/streaming is
803  * enabled AND it's a WAL-logged rel.
804  */
805  use_wal = XLogIsNeeded() && RelationNeedsWAL(NewHeap);
806 
807  /* use_wal off requires smgr_targblock be initially invalid */
809 
810  /*
811  * If both tables have TOAST tables, perform toast swap by content. It is
812  * possible that the old table has a toast table but the new one doesn't,
813  * if toastable columns have been dropped. In that case we have to do
814  * swap by links. This is okay because swap by content is only essential
815  * for system catalogs, and we don't support schema changes for them.
816  */
817  if (OldHeap->rd_rel->reltoastrelid && NewHeap->rd_rel->reltoastrelid)
818  {
819  *pSwapToastByContent = true;
820 
821  /*
822  * When doing swap by content, any toast pointers written into NewHeap
823  * must use the old toast table's OID, because that's where the toast
824  * data will eventually be found. Set this up by setting rd_toastoid.
825  * This also tells toast_save_datum() to preserve the toast value
826  * OIDs, which we want so as not to invalidate toast pointers in
827  * system catalog caches, and to avoid making multiple copies of a
828  * single toast value.
829  *
830  * Note that we must hold NewHeap open until we are done writing data,
831  * since the relcache will not guarantee to remember this setting once
832  * the relation is closed. Also, this technique depends on the fact
833  * that no one will try to read from the NewHeap until after we've
834  * finished writing it and swapping the rels --- otherwise they could
835  * follow the toast pointers to the wrong place. (It would actually
836  * work for values copied over from the old toast table, but not for
837  * any values that we toast which were previously not toasted.)
838  */
839  NewHeap->rd_toastoid = OldHeap->rd_rel->reltoastrelid;
840  }
841  else
842  *pSwapToastByContent = false;
843 
844  /*
845  * Compute xids used to freeze and weed out dead tuples and multixacts.
846  * Since we're going to rewrite the whole table anyway, there's no reason
847  * not to be aggressive about this.
848  */
849  vacuum_set_xid_limits(OldHeap, 0, 0, 0, 0,
850  &OldestXmin, &FreezeXid, NULL, &MultiXactCutoff,
851  NULL);
852 
853  /*
854  * FreezeXid will become the table's new relfrozenxid, and that mustn't go
855  * backwards, so take the max.
856  */
857  if (TransactionIdPrecedes(FreezeXid, OldHeap->rd_rel->relfrozenxid))
858  FreezeXid = OldHeap->rd_rel->relfrozenxid;
859 
860  /*
861  * MultiXactCutoff, similarly, shouldn't go backwards either.
862  */
863  if (MultiXactIdPrecedes(MultiXactCutoff, OldHeap->rd_rel->relminmxid))
864  MultiXactCutoff = OldHeap->rd_rel->relminmxid;
865 
866  /* return selected values to caller */
867  *pFreezeXid = FreezeXid;
868  *pCutoffMulti = MultiXactCutoff;
869 
870  /* Remember if it's a system catalog */
871  is_system_catalog = IsSystemRelation(OldHeap);
872 
873  /* Initialize the rewrite operation */
874  rwstate = begin_heap_rewrite(OldHeap, NewHeap, OldestXmin, FreezeXid,
875  MultiXactCutoff, use_wal);
876 
877  /*
878  * Decide whether to use an indexscan or seqscan-and-optional-sort to scan
879  * the OldHeap. We know how to use a sort to duplicate the ordering of a
880  * btree index, and will use seqscan-and-sort for that case if the planner
881  * tells us it's cheaper. Otherwise, always indexscan if an index is
882  * provided, else plain seqscan.
883  */
884  if (OldIndex != NULL && OldIndex->rd_rel->relam == BTREE_AM_OID)
885  use_sort = plan_cluster_use_sort(OIDOldHeap, OIDOldIndex);
886  else
887  use_sort = false;
888 
889  /* Set up sorting if wanted */
890  if (use_sort)
891  tuplesort = tuplesort_begin_cluster(oldTupDesc, OldIndex,
892  maintenance_work_mem, false);
893  else
894  tuplesort = NULL;
895 
896  /*
897  * Prepare to scan the OldHeap. To ensure we see recently-dead tuples
898  * that still need to be copied, we scan with SnapshotAny and use
899  * HeapTupleSatisfiesVacuum for the visibility test.
900  */
901  if (OldIndex != NULL && !use_sort)
902  {
903  heapScan = NULL;
904  indexScan = index_beginscan(OldHeap, OldIndex, SnapshotAny, 0, 0);
905  index_rescan(indexScan, NULL, 0, NULL, 0);
906  }
907  else
908  {
909  heapScan = heap_beginscan(OldHeap, SnapshotAny, 0, (ScanKey) NULL);
910  indexScan = NULL;
911  }
912 
913  /* Log what we're doing */
914  if (indexScan != NULL)
915  ereport(elevel,
916  (errmsg("clustering \"%s.%s\" using index scan on \"%s\"",
918  RelationGetRelationName(OldHeap),
919  RelationGetRelationName(OldIndex))));
920  else if (tuplesort != NULL)
921  ereport(elevel,
922  (errmsg("clustering \"%s.%s\" using sequential scan and sort",
924  RelationGetRelationName(OldHeap))));
925  else
926  ereport(elevel,
927  (errmsg("vacuuming \"%s.%s\"",
929  RelationGetRelationName(OldHeap))));
930 
931  /*
932  * Scan through the OldHeap, either in OldIndex order or sequentially;
933  * copy each tuple into the NewHeap, or transiently to the tuplesort
934  * module. Note that we don't bother sorting dead tuples (they won't get
935  * to the new table anyway).
936  */
937  for (;;)
938  {
939  HeapTuple tuple;
940  Buffer buf;
941  bool isdead;
942 
944 
945  if (indexScan != NULL)
946  {
947  tuple = index_getnext(indexScan, ForwardScanDirection);
948  if (tuple == NULL)
949  break;
950 
951  /* Since we used no scan keys, should never need to recheck */
952  if (indexScan->xs_recheck)
953  elog(ERROR, "CLUSTER does not support lossy index conditions");
954 
955  buf = indexScan->xs_cbuf;
956  }
957  else
958  {
959  tuple = heap_getnext(heapScan, ForwardScanDirection);
960  if (tuple == NULL)
961  break;
962 
963  buf = heapScan->rs_cbuf;
964  }
965 
967 
968  switch (HeapTupleSatisfiesVacuum(tuple, OldestXmin, buf))
969  {
970  case HEAPTUPLE_DEAD:
971  /* Definitely dead */
972  isdead = true;
973  break;
975  tups_recently_dead += 1;
976  /* fall through */
977  case HEAPTUPLE_LIVE:
978  /* Live or recently dead, must copy it */
979  isdead = false;
980  break;
982 
983  /*
984  * Since we hold exclusive lock on the relation, normally the
985  * only way to see this is if it was inserted earlier in our
986  * own transaction. However, it can happen in system
987  * catalogs, since we tend to release write lock before commit
988  * there. Give a warning if neither case applies; but in any
989  * case we had better copy it.
990  */
991  if (!is_system_catalog &&
993  elog(WARNING, "concurrent insert in progress within table \"%s\"",
994  RelationGetRelationName(OldHeap));
995  /* treat as live */
996  isdead = false;
997  break;
999 
1000  /*
1001  * Similar situation to INSERT_IN_PROGRESS case.
1002  */
1003  if (!is_system_catalog &&
1005  elog(WARNING, "concurrent delete in progress within table \"%s\"",
1006  RelationGetRelationName(OldHeap));
1007  /* treat as recently dead */
1008  tups_recently_dead += 1;
1009  isdead = false;
1010  break;
1011  default:
1012  elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result");
1013  isdead = false; /* keep compiler quiet */
1014  break;
1015  }
1016 
1018 
1019  if (isdead)
1020  {
1021  tups_vacuumed += 1;
1022  /* heap rewrite module still needs to see it... */
1023  if (rewrite_heap_dead_tuple(rwstate, tuple))
1024  {
1025  /* A previous recently-dead tuple is now known dead */
1026  tups_vacuumed += 1;
1027  tups_recently_dead -= 1;
1028  }
1029  continue;
1030  }
1031 
1032  num_tuples += 1;
1033  if (tuplesort != NULL)
1034  tuplesort_putheaptuple(tuplesort, tuple);
1035  else
1037  oldTupDesc, newTupDesc,
1038  values, isnull,
1039  NewHeap->rd_rel->relhasoids, rwstate);
1040  }
1041 
1042  if (indexScan != NULL)
1043  index_endscan(indexScan);
1044  if (heapScan != NULL)
1045  heap_endscan(heapScan);
1046 
1047  /*
1048  * In scan-and-sort mode, complete the sort, then read out all live tuples
1049  * from the tuplestore and write them to the new relation.
1050  */
1051  if (tuplesort != NULL)
1052  {
1053  tuplesort_performsort(tuplesort);
1054 
1055  for (;;)
1056  {
1057  HeapTuple tuple;
1058 
1060 
1061  tuple = tuplesort_getheaptuple(tuplesort, true);
1062  if (tuple == NULL)
1063  break;
1064 
1066  oldTupDesc, newTupDesc,
1067  values, isnull,
1068  NewHeap->rd_rel->relhasoids, rwstate);
1069  }
1070 
1071  tuplesort_end(tuplesort);
1072  }
1073 
1074  /* Write out any remaining tuples, and fsync if needed */
1075  end_heap_rewrite(rwstate);
1076 
1077  /* Reset rd_toastoid just to be tidy --- it shouldn't be looked at again */
1078  NewHeap->rd_toastoid = InvalidOid;
1079 
1080  /* Log what we did */
1081  ereport(elevel,
1082  (errmsg("\"%s\": found %.0f removable, %.0f nonremovable row versions in %u pages",
1083  RelationGetRelationName(OldHeap),
1084  tups_vacuumed, num_tuples,
1085  RelationGetNumberOfBlocks(OldHeap)),
1086  errdetail("%.0f dead row versions cannot be removed yet.\n"
1087  "%s.",
1088  tups_recently_dead,
1089  pg_rusage_show(&ru0))));
1090 
1091  /* Clean up */
1092  pfree(values);
1093  pfree(isnull);
1094 
1095  if (OldIndex != NULL)
1096  index_close(OldIndex, NoLock);
1097  heap_close(OldHeap, NoLock);
1098  heap_close(NewHeap, NoLock);
1099 }
#define HeapTupleHeaderGetUpdateXid(tup)
Definition: htup_details.h:359
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:87
void tuplesort_performsort(Tuplesortstate *state)
Definition: tuplesort.c:1763
void end_heap_rewrite(RewriteState state)
Definition: rewriteheap.c:312
HeapTuple tuplesort_getheaptuple(Tuplesortstate *state, bool forward)
Definition: tuplesort.c:2134
bool plan_cluster_use_sort(Oid tableOid, Oid indexOid)
Definition: planner.c:5240
void heap_endscan(HeapScanDesc scan)
Definition: heapam.c:1581
bool IsSystemRelation(Relation relation)
Definition: catalog.c:62
uint32 TransactionId
Definition: c.h:394
RewriteState begin_heap_rewrite(Relation old_heap, Relation new_heap, TransactionId oldest_xmin, TransactionId freeze_xid, MultiXactId cutoff_multi, bool use_wal)
Definition: rewriteheap.c:246
#define RelationGetDescr(relation)
Definition: rel.h:425
bool TransactionIdIsCurrentTransactionId(TransactionId xid)
Definition: xact.c:772
#define XLogIsNeeded()
Definition: xlog.h:145
#define BTREE_AM_OID
Definition: pg_am.h:70
HTSV_Result HeapTupleSatisfiesVacuum(HeapTuple htup, TransactionId OldestXmin, Buffer buffer)
Definition: tqual.c:1164
void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys)
Definition: indexam.c:310
static void reform_and_rewrite_tuple(HeapTuple tuple, TupleDesc oldTupDesc, TupleDesc newTupDesc, Datum *values, bool *isnull, bool newRelHasOids, RewriteState rwstate)
Definition: cluster.c:1702
#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:471
#define heap_close(r, l)
Definition: heapam.h:97
Form_pg_class rd_rel
Definition: rel.h:113
#define OidIsValid(objectId)
Definition: c.h:534
#define RelationGetTargetBlock(relation)
Definition: rel.h:485
int natts
Definition: tupdesc.h:73
HeapTupleHeader t_data
Definition: htup.h:67
void pg_rusage_init(PGRUsage *ru0)
Definition: pg_rusage.c:27
void pfree(void *pointer)
Definition: mcxt.c:992
Buffer xs_cbuf
Definition: relscan.h:113
#define ERROR
Definition: elog.h:43
#define DEBUG2
Definition: elog.h:24
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3006
#define NoLock
Definition: lockdefs.h:34
static char * buf
Definition: pg_test_fsync.c:65
Oid rd_toastoid
Definition: rel.h:211
int errdetail(const char *fmt,...)
Definition: elog.c:873
static MultiXactId MultiXactCutoff
Definition: vacuumlazy.c:140
const char * pg_rusage_show(const PGRUsage *ru0)
Definition: pg_rusage.c:40
#define RelationGetRelationName(relation)
Definition: rel.h:433
static TransactionId OldestXmin
Definition: vacuumlazy.c:138
void index_endscan(IndexScanDesc scan)
Definition: indexam.c:340
#define ereport(elevel, rest)
Definition: elog.h:122
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
static int verbose
Definition: pg_basebackup.c:86
#define WARNING
Definition: elog.h:40
static int elevel
Definition: vacuumlazy.c:136
uintptr_t Datum
Definition: postgres.h:374
#define SnapshotAny
Definition: tqual.h:28
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3529
HeapTuple heap_getnext(HeapScanDesc scan, ScanDirection direction)
Definition: heapam.c:1781
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:199
#define InvalidOid
Definition: postgres_ext.h:36
int maintenance_work_mem
Definition: globals.c:113
Buffer rs_cbuf
Definition: relscan.h:70
TransactionId MultiXactId
Definition: c.h:404
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define HeapTupleHeaderGetXmin(tup)
Definition: htup_details.h:307
#define InvalidBlockNumber
Definition: block.h:33
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3135
#define RelationNeedsWAL(relation)
Definition: rel.h:502
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:176
Tuplesortstate * tuplesort_begin_cluster(TupleDesc tupDesc, Relation indexRel, int workMem, bool randomAccess)
Definition: tuplesort.c:825
static Datum values[MAXATTR]
Definition: bootstrap.c:162
bool rewrite_heap_dead_tuple(RewriteState state, HeapTuple old_tuple)
Definition: rewriteheap.c:576
#define AccessExclusiveLock
Definition: lockdefs.h:46
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
void tuplesort_putheaptuple(Tuplesortstate *state, HeapTuple tup)
Definition: tuplesort.c:1376
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:88
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
#define elog
Definition: elog.h:219
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:105
void tuplesort_end(Tuplesortstate *state)
Definition: tuplesort.c:1157
HeapScanDesc heap_beginscan(Relation relation, Snapshot snapshot, int nkeys, ScanKey key)
Definition: heapam.c:1394
int Buffer
Definition: buf.h:23
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:151
HeapTuple index_getnext(IndexScanDesc scan, ScanDirection direction)
Definition: indexam.c:659
IndexScanDesc index_beginscan(Relation heapRelation, Relation indexRelation, Snapshot snapshot, int nkeys, int norderbys)
Definition: indexam.c:221
#define RelationGetNamespace(relation)
Definition: rel.h:440
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 1467 of file cluster.c.

References AccessShareLock, CacheInvalidateCatalog(), CatalogTupleUpdate(), DROP_RESTRICT, elog, ERROR, GETSTRUCT, heap_close, heap_open(), HeapTupleIsValid, i, NAMEDATALEN, NoLock, ObjectIdGetDatum, OidIsValid, PERFORM_DELETION_INTERNAL, performDeletion(), 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(), RelationMapRemoveMapping(), RelationRelationId, RELOID, RELPERSISTENCE_PERMANENT, RELPERSISTENCE_UNLOGGED, RenameRelationInternal(), RowExclusiveLock, SearchSysCacheCopy1, snprintf(), swap_relation_files(), HeapTupleData::t_self, and toast_get_valid_index().

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

1475 {
1476  ObjectAddress object;
1477  Oid mapped_tables[4];
1478  int reindex_flags;
1479  int i;
1480 
1481  /* Zero out possible results from swapped_relation_files */
1482  memset(mapped_tables, 0, sizeof(mapped_tables));
1483 
1484  /*
1485  * Swap the contents of the heap relations (including any toast tables).
1486  * Also set old heap's relfrozenxid to frozenXid.
1487  */
1488  swap_relation_files(OIDOldHeap, OIDNewHeap,
1489  (OIDOldHeap == RelationRelationId),
1490  swap_toast_by_content, is_internal,
1491  frozenXid, cutoffMulti, mapped_tables);
1492 
1493  /*
1494  * If it's a system catalog, queue an sinval message to flush all
1495  * catcaches on the catalog when we reach CommandCounterIncrement.
1496  */
1497  if (is_system_catalog)
1498  CacheInvalidateCatalog(OIDOldHeap);
1499 
1500  /*
1501  * Rebuild each index on the relation (but not the toast table, which is
1502  * all-new at this point). It is important to do this before the DROP
1503  * step because if we are processing a system catalog that will be used
1504  * during DROP, we want to have its indexes available. There is no
1505  * advantage to the other order anyway because this is all transactional,
1506  * so no chance to reclaim disk space before commit. We do not need a
1507  * final CommandCounterIncrement() because reindex_relation does it.
1508  *
1509  * Note: because index_build is called via reindex_relation, it will never
1510  * set indcheckxmin true for the indexes. This is OK even though in some
1511  * sense we are building new indexes rather than rebuilding existing ones,
1512  * because the new heap won't contain any HOT chains at all, let alone
1513  * broken ones, so it can't be necessary to set indcheckxmin.
1514  */
1515  reindex_flags = REINDEX_REL_SUPPRESS_INDEX_USE;
1516  if (check_constraints)
1517  reindex_flags |= REINDEX_REL_CHECK_CONSTRAINTS;
1518 
1519  /*
1520  * Ensure that the indexes have the same persistence as the parent
1521  * relation.
1522  */
1523  if (newrelpersistence == RELPERSISTENCE_UNLOGGED)
1524  reindex_flags |= REINDEX_REL_FORCE_INDEXES_UNLOGGED;
1525  else if (newrelpersistence == RELPERSISTENCE_PERMANENT)
1526  reindex_flags |= REINDEX_REL_FORCE_INDEXES_PERMANENT;
1527 
1528  reindex_relation(OIDOldHeap, reindex_flags, 0);
1529 
1530  /*
1531  * If the relation being rebuild is pg_class, swap_relation_files()
1532  * couldn't update pg_class's own pg_class entry (check comments in
1533  * swap_relation_files()), thus relfrozenxid was not updated. That's
1534  * annoying because a potential reason for doing a VACUUM FULL is a
1535  * imminent or actual anti-wraparound shutdown. So, now that we can
1536  * access the new relation using it's indices, update relfrozenxid.
1537  * pg_class doesn't have a toast relation, so we don't need to update the
1538  * corresponding toast relation. Not that there's little point moving all
1539  * relfrozenxid updates here since swap_relation_files() needs to write to
1540  * pg_class for non-mapped relations anyway.
1541  */
1542  if (OIDOldHeap == RelationRelationId)
1543  {
1544  Relation relRelation;
1545  HeapTuple reltup;
1546  Form_pg_class relform;
1547 
1549 
1550  reltup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(OIDOldHeap));
1551  if (!HeapTupleIsValid(reltup))
1552  elog(ERROR, "cache lookup failed for relation %u", OIDOldHeap);
1553  relform = (Form_pg_class) GETSTRUCT(reltup);
1554 
1555  relform->relfrozenxid = frozenXid;
1556  relform->relminmxid = cutoffMulti;
1557 
1558  CatalogTupleUpdate(relRelation, &reltup->t_self, reltup);
1559 
1560  heap_close(relRelation, RowExclusiveLock);
1561  }
1562 
1563  /* Destroy new heap with old filenode */
1564  object.classId = RelationRelationId;
1565  object.objectId = OIDNewHeap;
1566  object.objectSubId = 0;
1567 
1568  /*
1569  * The new relation is local to our transaction and we know nothing
1570  * depends on it, so DROP_RESTRICT should be OK.
1571  */
1573 
1574  /* performDeletion does CommandCounterIncrement at end */
1575 
1576  /*
1577  * Now we must remove any relation mapping entries that we set up for the
1578  * transient table, as well as its toast table and toast index if any. If
1579  * we fail to do this before commit, the relmapper will complain about new
1580  * permanent map entries being added post-bootstrap.
1581  */
1582  for (i = 0; OidIsValid(mapped_tables[i]); i++)
1583  RelationMapRemoveMapping(mapped_tables[i]);
1584 
1585  /*
1586  * At this point, everything is kosher except that, if we did toast swap
1587  * by links, the toast table's name corresponds to the transient table.
1588  * The name is irrelevant to the backend because it's referenced by OID,
1589  * but users looking at the catalogs could be confused. Rename it to
1590  * prevent this problem.
1591  *
1592  * Note no lock required on the relation, because we already hold an
1593  * exclusive lock on it.
1594  */
1595  if (!swap_toast_by_content)
1596  {
1597  Relation newrel;
1598 
1599  newrel = heap_open(OIDOldHeap, NoLock);
1600  if (OidIsValid(newrel->rd_rel->reltoastrelid))
1601  {
1602  Oid toastidx;
1603  char NewToastName[NAMEDATALEN];
1604 
1605  /* Get the associated valid index to be renamed */
1606  toastidx = toast_get_valid_index(newrel->rd_rel->reltoastrelid,
1607  AccessShareLock);
1608 
1609  /* rename the toast table ... */
1610  snprintf(NewToastName, NAMEDATALEN, "pg_toast_%u",
1611  OIDOldHeap);
1612  RenameRelationInternal(newrel->rd_rel->reltoastrelid,
1613  NewToastName, true);
1614 
1615  /* ... and its valid index too. */
1616  snprintf(NewToastName, NAMEDATALEN, "pg_toast_%u_index",
1617  OIDOldHeap);
1618 
1619  RenameRelationInternal(toastidx,
1620  NewToastName, true);
1621  }
1622  relation_close(newrel, NoLock);
1623  }
1624 }
void RenameRelationInternal(Oid myrelid, const char *newrelname, bool is_internal)
Definition: tablecmds.c:2854
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define RELPERSISTENCE_UNLOGGED
Definition: pg_class.h:171
#define RelationRelationId
Definition: pg_class.h:29
Oid toast_get_valid_index(Oid toastoid, LOCKMODE lock)
Definition: tuptoaster.c:1363
#define AccessShareLock
Definition: lockdefs.h:36
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1263
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define heap_close(r, l)
Definition: heapam.h:97
#define REINDEX_REL_SUPPRESS_INDEX_USE
Definition: index.h:123
Form_pg_class rd_rel
Definition: rel.h:113
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:534
#define RELPERSISTENCE_PERMANENT
Definition: pg_class.h:170
#define NAMEDATALEN
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#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:301
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
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:1128
void CacheInvalidateCatalog(Oid catalogId)
Definition: inval.c:1201
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define REINDEX_REL_FORCE_INDEXES_UNLOGGED
Definition: index.h:125
void RelationMapRemoveMapping(Oid relationId)
Definition: relmapper.c:356
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:210
#define REINDEX_REL_CHECK_CONSTRAINTS
Definition: index.h:124
FormData_pg_class * Form_pg_class
Definition: pg_class.h:95
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:158
int i
#define REINDEX_REL_FORCE_INDEXES_PERMANENT
Definition: index.h:126
#define elog
Definition: elog.h:219
bool reindex_relation(Oid relid, int flags, int options)
Definition: index.c:3515
#define PERFORM_DELETION_INTERNAL
Definition: dependency.h:173
static List * get_tables_to_cluster ( MemoryContext  cluster_context)
static

Definition at line 1634 of file cluster.c.

References AccessShareLock, Anum_pg_index_indisclustered, BoolGetDatum, BTEqualStrategyNumber, ForwardScanDirection, GETSTRUCT, GetUserId(), heap_beginscan_catalog(), heap_endscan(), heap_getnext(), heap_open(), RelToCluster::indexOid, IndexRelationId, lcons(), MemoryContextSwitchTo(), NIL, NULL, palloc(), pg_class_ownercheck(), relation_close(), ScanKeyInit(), and RelToCluster::tableOid.

Referenced by cluster().

1635 {
1636  Relation indRelation;
1637  HeapScanDesc scan;
1638  ScanKeyData entry;
1639  HeapTuple indexTuple;
1641  MemoryContext old_context;
1642  RelToCluster *rvtc;
1643  List *rvs = NIL;
1644 
1645  /*
1646  * Get all indexes that have indisclustered set and are owned by
1647  * appropriate user. System relations or nailed-in relations cannot ever
1648  * have indisclustered set, because CLUSTER will refuse to set it when
1649  * called with one of them as argument.
1650  */
1651  indRelation = heap_open(IndexRelationId, AccessShareLock);
1652  ScanKeyInit(&entry,
1654  BTEqualStrategyNumber, F_BOOLEQ,
1655  BoolGetDatum(true));
1656  scan = heap_beginscan_catalog(indRelation, 1, &entry);
1657  while ((indexTuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1658  {
1659  index = (Form_pg_index) GETSTRUCT(indexTuple);
1660 
1661  if (!pg_class_ownercheck(index->indrelid, GetUserId()))
1662  continue;
1663 
1664  /*
1665  * We have to build the list in a different memory context so it will
1666  * survive the cross-transaction processing
1667  */
1668  old_context = MemoryContextSwitchTo(cluster_context);
1669 
1670  rvtc = (RelToCluster *) palloc(sizeof(RelToCluster));
1671  rvtc->tableOid = index->indrelid;
1672  rvtc->indexOid = index->indexrelid;
1673  rvs = lcons(rvtc, rvs);
1674 
1675  MemoryContextSwitchTo(old_context);
1676  }
1677  heap_endscan(scan);
1678 
1679  relation_close(indRelation, AccessShareLock);
1680 
1681  return rvs;
1682 }
#define NIL
Definition: pg_list.h:69
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
void heap_endscan(HeapScanDesc scan)
Definition: heapam.c:1581
Oid GetUserId(void)
Definition: miscinit.c:282
#define IndexRelationId
Definition: pg_index.h:29
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define AccessShareLock
Definition: lockdefs.h:36
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1263
Oid tableOid
Definition: cluster.c:65
Definition: type.h:90
HeapScanDesc heap_beginscan_catalog(Relation relation, int nkeys, ScanKey key)
Definition: heapam.c:1402
FormData_pg_index * Form_pg_index
Definition: pg_index.h:67
HeapTuple heap_getnext(HeapScanDesc scan, ScanDirection direction)
Definition: heapam.c:1781
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
#define BoolGetDatum(X)
Definition: postgres.h:410
List * lcons(void *datum, List *list)
Definition: list.c:259
#define NULL
Definition: c.h:226
bool pg_class_ownercheck(Oid class_oid, Oid roleid)
Definition: aclchk.c:4521
Oid indexOid
Definition: cluster.c:66
void * palloc(Size size)
Definition: mcxt.c:891
#define Anum_pg_index_indisclustered
Definition: pg_index.h:81
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Definition: pg_list.h:45
#define BTEqualStrategyNumber
Definition: stratnum.h:31
Oid make_new_heap ( Oid  OIDOldHeap,
Oid  NewTableSpace,
char  relpersistence,
LOCKMODE  lockmode 
)

Definition at line 606 of file cluster.c.

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

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

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

Definition at line 478 of file cluster.c.

References CatalogTupleUpdate(), elog, ERROR, GETSTRUCT, heap_close, heap_freetuple(), heap_open(), HeapTupleIsValid, IndexIsValid, IndexRelationId, INDEXRELID, InvalidOid, InvokeObjectPostAlterHookArg, lfirst_oid, ObjectIdGetDatum, OidIsValid, RelationGetIndexList(), ReleaseSysCache(), RowExclusiveLock, SearchSysCache1, SearchSysCacheCopy1, and HeapTupleData::t_self.

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

479 {
480  HeapTuple indexTuple;
481  Form_pg_index indexForm;
482  Relation pg_index;
483  ListCell *index;
484 
485  /*
486  * If the index is already marked clustered, no need to do anything.
487  */
488  if (OidIsValid(indexOid))
489  {
490  indexTuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexOid));
491  if (!HeapTupleIsValid(indexTuple))
492  elog(ERROR, "cache lookup failed for index %u", indexOid);
493  indexForm = (Form_pg_index) GETSTRUCT(indexTuple);
494 
495  if (indexForm->indisclustered)
496  {
497  ReleaseSysCache(indexTuple);
498  return;
499  }
500 
501  ReleaseSysCache(indexTuple);
502  }
503 
504  /*
505  * Check each index of the relation and set/clear the bit as needed.
506  */
508 
509  foreach(index, RelationGetIndexList(rel))
510  {
511  Oid thisIndexOid = lfirst_oid(index);
512 
513  indexTuple = SearchSysCacheCopy1(INDEXRELID,
514  ObjectIdGetDatum(thisIndexOid));
515  if (!HeapTupleIsValid(indexTuple))
516  elog(ERROR, "cache lookup failed for index %u", thisIndexOid);
517  indexForm = (Form_pg_index) GETSTRUCT(indexTuple);
518 
519  /*
520  * Unset the bit if set. We know it's wrong because we checked this
521  * earlier.
522  */
523  if (indexForm->indisclustered)
524  {
525  indexForm->indisclustered = false;
526  CatalogTupleUpdate(pg_index, &indexTuple->t_self, indexTuple);
527  }
528  else if (thisIndexOid == indexOid)
529  {
530  /* this was checked earlier, but let's be real sure */
531  if (!IndexIsValid(indexForm))
532  elog(ERROR, "cannot cluster on invalid index %u", indexOid);
533  indexForm->indisclustered = true;
534  CatalogTupleUpdate(pg_index, &indexTuple->t_self, indexTuple);
535  }
536 
538  InvalidOid, is_internal);
539 
540  heap_freetuple(indexTuple);
541  }
542 
543  heap_close(pg_index, RowExclusiveLock);
544 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define IndexIsValid(indexForm)
Definition: pg_index.h:107
#define IndexRelationId
Definition: pg_index.h:29
#define heap_close(r, l)
Definition: heapam.h:97
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1374
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:534
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:149
Definition: type.h:90
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define InvokeObjectPostAlterHookArg(classId, objectId, subId,auxiliaryId, is_internal)
Definition: objectaccess.h:166
#define RowExclusiveLock
Definition: lockdefs.h:38
FormData_pg_index * Form_pg_index
Definition: pg_index.h:67
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1083
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:210
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4336
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:158
#define elog
Definition: elog.h:219
#define lfirst_oid(lc)
Definition: pg_list.h:108
static void rebuild_relation ( Relation  OldHeap,
Oid  indexOid,
bool  verbose 
)
static

Definition at line 555 of file cluster.c.

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

Referenced by cluster_rel().

556 {
557  Oid tableOid = RelationGetRelid(OldHeap);
558  Oid tableSpace = OldHeap->rd_rel->reltablespace;
559  Oid OIDNewHeap;
560  bool is_system_catalog;
561  bool swap_toast_by_content;
562  TransactionId frozenXid;
563  MultiXactId cutoffMulti;
564 
565  /* Mark the correct index as clustered */
566  if (OidIsValid(indexOid))
567  mark_index_clustered(OldHeap, indexOid, true);
568 
569  /* Remember if it's a system catalog */
570  is_system_catalog = IsSystemRelation(OldHeap);
571 
572  /* Close relcache entry, but keep lock until transaction commit */
573  heap_close(OldHeap, NoLock);
574 
575  /* Create the transient table that will receive the re-ordered data */
576  OIDNewHeap = make_new_heap(tableOid, tableSpace,
577  OldHeap->rd_rel->relpersistence,
579 
580  /* Copy the heap data into the new table in the desired order */
581  copy_heap_data(OIDNewHeap, tableOid, indexOid, verbose,
582  &swap_toast_by_content, &frozenXid, &cutoffMulti);
583 
584  /*
585  * Swap the physical files of the target and transient tables, then
586  * rebuild the target's indexes and throw away the transient table.
587  */
588  finish_heap_swap(tableOid, OIDNewHeap, is_system_catalog,
589  swap_toast_by_content, false, true,
590  frozenXid, cutoffMulti,
591  OldHeap->rd_rel->relpersistence);
592 }
bool IsSystemRelation(Relation relation)
Definition: catalog.c:62
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:1467
uint32 TransactionId
Definition: c.h:394
#define heap_close(r, l)
Definition: heapam.h:97
Form_pg_class rd_rel
Definition: rel.h:113
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:534
#define NoLock
Definition: lockdefs.h:34
static int verbose
Definition: pg_basebackup.c:86
void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal)
Definition: cluster.c:478
static void copy_heap_data(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex, bool verbose, bool *pSwapToastByContent, TransactionId *pFreezeXid, MultiXactId *pCutoffMulti)
Definition: cluster.c:732
TransactionId MultiXactId
Definition: c.h:404
#define AccessExclusiveLock
Definition: lockdefs.h:46
#define RelationGetRelid(relation)
Definition: rel.h:413
Oid make_new_heap(Oid OIDOldHeap, Oid NewTableSpace, char relpersistence, LOCKMODE lockmode)
Definition: cluster.c:606
static void reform_and_rewrite_tuple ( HeapTuple  tuple,
TupleDesc  oldTupDesc,
TupleDesc  newTupDesc,
Datum values,
bool isnull,
bool  newRelHasOids,
RewriteState  rwstate 
)
static

Definition at line 1702 of file cluster.c.

References tupleDesc::attrs, heap_deform_tuple(), heap_form_tuple(), heap_freetuple(), HeapTupleGetOid, HeapTupleSetOid, i, tupleDesc::natts, and rewrite_heap_tuple().

Referenced by copy_heap_data().

1706 {
1707  HeapTuple copiedTuple;
1708  int i;
1709 
1710  heap_deform_tuple(tuple, oldTupDesc, values, isnull);
1711 
1712  /* Be sure to null out any dropped columns */
1713  for (i = 0; i < newTupDesc->natts; i++)
1714  {
1715  if (newTupDesc->attrs[i]->attisdropped)
1716  isnull[i] = true;
1717  }
1718 
1719  copiedTuple = heap_form_tuple(newTupDesc, values, isnull);
1720 
1721  /* Preserve OID, if any */
1722  if (newRelHasOids)
1723  HeapTupleSetOid(copiedTuple, HeapTupleGetOid(tuple));
1724 
1725  /* The heap rewrite module does the rest */
1726  rewrite_heap_tuple(rwstate, tuple, copiedTuple);
1727 
1728  heap_freetuple(copiedTuple);
1729 }
Form_pg_attribute * attrs
Definition: tupdesc.h:74
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:692
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1374
int natts
Definition: tupdesc.h:73
#define HeapTupleSetOid(tuple, oid)
Definition: htup_details.h:698
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:935
static Datum values[MAXATTR]
Definition: bootstrap.c:162
int i
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:695
void rewrite_heap_tuple(RewriteState state, HeapTuple old_tuple, HeapTuple new_tuple)
Definition: rewriteheap.c:378
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 1128 of file cluster.c.

References AccessExclusiveLock, Assert, CacheInvalidateRelcacheByTuple(), CatalogCloseIndexes(), CatalogOpenIndexes(), CatalogTupleUpdateWithInfo(), ObjectAddress::classId, deleteDependencyRecordsFor(), DEPENDENCY_INTERNAL, elog, ERROR, GETSTRUCT, heap_close, heap_freetuple(), heap_open(), HeapTupleIsValid, InvalidMultiXactId, InvalidOid, InvalidTransactionId, InvokeObjectPostAlterHookArg, IsSystemClass(), MultiXactIdIsValid, NameStr, ObjectAddress::objectId, ObjectIdGetDatum, ObjectAddress::objectSubId, OidIsValid, recordDependencyOn(), RelationCloseSmgrByOid(), RelationMapOidToFilenode(), RelationMapUpdateMap(), RelationRelationId, RELKIND_INDEX, RELKIND_TOASTVALUE, RELOID, RowExclusiveLock, SearchSysCacheCopy1, HeapTupleData::t_self, toast_get_valid_index(), and TransactionIdIsNormal.

Referenced by finish_heap_swap().

1134 {
1135  Relation relRelation;
1136  HeapTuple reltup1,
1137  reltup2;
1138  Form_pg_class relform1,
1139  relform2;
1140  Oid relfilenode1,
1141  relfilenode2;
1142  Oid swaptemp;
1143  char swptmpchr;
1144 
1145  /* We need writable copies of both pg_class tuples. */
1147 
1149  if (!HeapTupleIsValid(reltup1))
1150  elog(ERROR, "cache lookup failed for relation %u", r1);
1151  relform1 = (Form_pg_class) GETSTRUCT(reltup1);
1152 
1154  if (!HeapTupleIsValid(reltup2))
1155  elog(ERROR, "cache lookup failed for relation %u", r2);
1156  relform2 = (Form_pg_class) GETSTRUCT(reltup2);
1157 
1158  relfilenode1 = relform1->relfilenode;
1159  relfilenode2 = relform2->relfilenode;
1160 
1161  if (OidIsValid(relfilenode1) && OidIsValid(relfilenode2))
1162  {
1163  /*
1164  * Normal non-mapped relations: swap relfilenodes, reltablespaces,
1165  * relpersistence
1166  */
1167  Assert(!target_is_pg_class);
1168 
1169  swaptemp = relform1->relfilenode;
1170  relform1->relfilenode = relform2->relfilenode;
1171  relform2->relfilenode = swaptemp;
1172 
1173  swaptemp = relform1->reltablespace;
1174  relform1->reltablespace = relform2->reltablespace;
1175  relform2->reltablespace = swaptemp;
1176 
1177  swptmpchr = relform1->relpersistence;
1178  relform1->relpersistence = relform2->relpersistence;
1179  relform2->relpersistence = swptmpchr;
1180 
1181  /* Also swap toast links, if we're swapping by links */
1182  if (!swap_toast_by_content)
1183  {
1184  swaptemp = relform1->reltoastrelid;
1185  relform1->reltoastrelid = relform2->reltoastrelid;
1186  relform2->reltoastrelid = swaptemp;
1187  }
1188  }
1189  else
1190  {
1191  /*
1192  * Mapped-relation case. Here we have to swap the relation mappings
1193  * instead of modifying the pg_class columns. Both must be mapped.
1194  */
1195  if (OidIsValid(relfilenode1) || OidIsValid(relfilenode2))
1196  elog(ERROR, "cannot swap mapped relation \"%s\" with non-mapped relation",
1197  NameStr(relform1->relname));
1198 
1199  /*
1200  * We can't change the tablespace nor persistence of a mapped rel, and
1201  * we can't handle toast link swapping for one either, because we must
1202  * not apply any critical changes to its pg_class row. These cases
1203  * should be prevented by upstream permissions tests, so these checks
1204  * are non-user-facing emergency backstop.
1205  */
1206  if (relform1->reltablespace != relform2->reltablespace)
1207  elog(ERROR, "cannot change tablespace of mapped relation \"%s\"",
1208  NameStr(relform1->relname));
1209  if (relform1->relpersistence != relform2->relpersistence)
1210  elog(ERROR, "cannot change persistence of mapped relation \"%s\"",
1211  NameStr(relform1->relname));
1212  if (!swap_toast_by_content &&
1213  (relform1->reltoastrelid || relform2->reltoastrelid))
1214  elog(ERROR, "cannot swap toast by links for mapped relation \"%s\"",
1215  NameStr(relform1->relname));
1216 
1217  /*
1218  * Fetch the mappings --- shouldn't fail, but be paranoid
1219  */
1220  relfilenode1 = RelationMapOidToFilenode(r1, relform1->relisshared);
1221  if (!OidIsValid(relfilenode1))
1222  elog(ERROR, "could not find relation mapping for relation \"%s\", OID %u",
1223  NameStr(relform1->relname), r1);
1224  relfilenode2 = RelationMapOidToFilenode(r2, relform2->relisshared);
1225  if (!OidIsValid(relfilenode2))
1226  elog(ERROR, "could not find relation mapping for relation \"%s\", OID %u",
1227  NameStr(relform2->relname), r2);
1228 
1229  /*
1230  * Send replacement mappings to relmapper. Note these won't actually
1231  * take effect until CommandCounterIncrement.
1232  */
1233  RelationMapUpdateMap(r1, relfilenode2, relform1->relisshared, false);
1234  RelationMapUpdateMap(r2, relfilenode1, relform2->relisshared, false);
1235 
1236  /* Pass OIDs of mapped r2 tables back to caller */
1237  *mapped_tables++ = r2;
1238  }
1239 
1240  /*
1241  * In the case of a shared catalog, these next few steps will only affect
1242  * our own database's pg_class row; but that's okay, because they are all
1243  * noncritical updates. That's also an important fact for the case of a
1244  * mapped catalog, because it's possible that we'll commit the map change
1245  * and then fail to commit the pg_class update.
1246  */
1247 
1248  /* set rel1's frozen Xid and minimum MultiXid */
1249  if (relform1->relkind != RELKIND_INDEX)
1250  {
1251  Assert(TransactionIdIsNormal(frozenXid));
1252  relform1->relfrozenxid = frozenXid;
1253  Assert(MultiXactIdIsValid(cutoffMulti));
1254  relform1->relminmxid = cutoffMulti;
1255  }
1256 
1257  /* swap size statistics too, since new rel has freshly-updated stats */
1258  {
1259  int32 swap_pages;
1260  float4 swap_tuples;
1261  int32 swap_allvisible;
1262 
1263  swap_pages = relform1->relpages;
1264  relform1->relpages = relform2->relpages;
1265  relform2->relpages = swap_pages;
1266 
1267  swap_tuples = relform1->reltuples;
1268  relform1->reltuples = relform2->reltuples;
1269  relform2->reltuples = swap_tuples;
1270 
1271  swap_allvisible = relform1->relallvisible;
1272  relform1->relallvisible = relform2->relallvisible;
1273  relform2->relallvisible = swap_allvisible;
1274  }
1275 
1276  /*
1277  * Update the tuples in pg_class --- unless the target relation of the
1278  * swap is pg_class itself. In that case, there is zero point in making
1279  * changes because we'd be updating the old data that we're about to throw
1280  * away. Because the real work being done here for a mapped relation is
1281  * just to change the relation map settings, it's all right to not update
1282  * the pg_class rows in this case. The most important changes will instead
1283  * performed later, in finish_heap_swap() itself.
1284  */
1285  if (!target_is_pg_class)
1286  {
1287  CatalogIndexState indstate;
1288 
1289  indstate = CatalogOpenIndexes(relRelation);
1290  CatalogTupleUpdateWithInfo(relRelation, &reltup1->t_self, reltup1,
1291  indstate);
1292  CatalogTupleUpdateWithInfo(relRelation, &reltup2->t_self, reltup2,
1293  indstate);
1294  CatalogCloseIndexes(indstate);
1295  }
1296  else
1297  {
1298  /* no update ... but we do still need relcache inval */
1301  }
1302 
1303  /*
1304  * Post alter hook for modified relations. The change to r2 is always
1305  * internal, but r1 depends on the invocation context.
1306  */
1308  InvalidOid, is_internal);
1310  InvalidOid, true);
1311 
1312  /*
1313  * If we have toast tables associated with the relations being swapped,
1314  * deal with them too.
1315  */
1316  if (relform1->reltoastrelid || relform2->reltoastrelid)
1317  {
1318  if (swap_toast_by_content)
1319  {
1320  if (relform1->reltoastrelid && relform2->reltoastrelid)
1321  {
1322  /* Recursively swap the contents of the toast tables */
1323  swap_relation_files(relform1->reltoastrelid,
1324  relform2->reltoastrelid,
1325  target_is_pg_class,
1326  swap_toast_by_content,
1327  is_internal,
1328  frozenXid,
1329  cutoffMulti,
1330  mapped_tables);
1331  }
1332  else
1333  {
1334  /* caller messed up */
1335  elog(ERROR, "cannot swap toast files by content when there's only one");
1336  }
1337  }
1338  else
1339  {
1340  /*
1341  * We swapped the ownership links, so we need to change dependency
1342  * data to match.
1343  *
1344  * NOTE: it is possible that only one table has a toast table.
1345  *
1346  * NOTE: at present, a TOAST table's only dependency is the one on
1347  * its owning table. If more are ever created, we'd need to use
1348  * something more selective than deleteDependencyRecordsFor() to
1349  * get rid of just the link we want.
1350  */
1351  ObjectAddress baseobject,
1352  toastobject;
1353  long count;
1354 
1355  /*
1356  * We disallow this case for system catalogs, to avoid the
1357  * possibility that the catalog we're rebuilding is one of the
1358  * ones the dependency changes would change. It's too late to be
1359  * making any data changes to the target catalog.
1360  */
1361  if (IsSystemClass(r1, relform1))
1362  elog(ERROR, "cannot swap toast files by links for system catalogs");
1363 
1364  /* Delete old dependencies */
1365  if (relform1->reltoastrelid)
1366  {
1368  relform1->reltoastrelid,
1369  false);
1370  if (count != 1)
1371  elog(ERROR, "expected one dependency record for TOAST table, found %ld",
1372  count);
1373  }
1374  if (relform2->reltoastrelid)
1375  {
1377  relform2->reltoastrelid,
1378  false);
1379  if (count != 1)
1380  elog(ERROR, "expected one dependency record for TOAST table, found %ld",
1381  count);
1382  }
1383 
1384  /* Register new dependencies */
1385  baseobject.classId = RelationRelationId;
1386  baseobject.objectSubId = 0;
1387  toastobject.classId = RelationRelationId;
1388  toastobject.objectSubId = 0;
1389 
1390  if (relform1->reltoastrelid)
1391  {
1392  baseobject.objectId = r1;
1393  toastobject.objectId = relform1->reltoastrelid;
1394  recordDependencyOn(&toastobject, &baseobject,
1396  }
1397 
1398  if (relform2->reltoastrelid)
1399  {
1400  baseobject.objectId = r2;
1401  toastobject.objectId = relform2->reltoastrelid;
1402  recordDependencyOn(&toastobject, &baseobject,
1404  }
1405  }
1406  }
1407 
1408  /*
1409  * If we're swapping two toast tables by content, do the same for their
1410  * valid index. The swap can actually be safely done only if the relations
1411  * have indexes.
1412  */
1413  if (swap_toast_by_content &&
1414  relform1->relkind == RELKIND_TOASTVALUE &&
1415  relform2->relkind == RELKIND_TOASTVALUE)
1416  {
1417  Oid toastIndex1,
1418  toastIndex2;
1419 
1420  /* Get valid index for each relation */
1421  toastIndex1 = toast_get_valid_index(r1,
1423  toastIndex2 = toast_get_valid_index(r2,
1425 
1426  swap_relation_files(toastIndex1,
1427  toastIndex2,
1428  target_is_pg_class,
1429  swap_toast_by_content,
1430  is_internal,
1433  mapped_tables);
1434  }
1435 
1436  /* Clean up. */
1437  heap_freetuple(reltup1);
1438  heap_freetuple(reltup2);
1439 
1440  heap_close(relRelation, RowExclusiveLock);
1441 
1442  /*
1443  * Close both relcache entries' smgr links. We need this kluge because
1444  * both links will be invalidated during upcoming CommandCounterIncrement.
1445  * Whichever of the rels is the second to be cleared will have a dangling
1446  * reference to the other's smgr entry. Rather than trying to avoid this
1447  * by ordering operations just so, it's easiest to close the links first.
1448  * (Fortunately, since one of the entries is local in our transaction,
1449  * it's sufficient to clear out our own relcache this way; the problem
1450  * cannot arise for other backends when they see our update on the
1451  * non-transient relation.)
1452  *
1453  * Caution: the placement of this step interacts with the decision to
1454  * handle toast rels by recursion. When we are trying to rebuild pg_class
1455  * itself, the smgr close on pg_class must happen after all accesses in
1456  * this function.
1457  */
1460 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define RelationRelationId
Definition: pg_class.h:29
long deleteDependencyRecordsFor(Oid classId, Oid objectId, bool skipExtensionDeps)
Definition: pg_depend.c:191
Oid toast_get_valid_index(Oid toastoid, LOCKMODE lock)
Definition: tuptoaster.c:1363
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:44
#define heap_close(r, l)
Definition: heapam.h:97
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1374
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:534
signed int int32
Definition: c.h:253
bool IsSystemClass(Oid relid, Form_pg_class reltuple)
Definition: catalog.c:74
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define InvokeObjectPostAlterHookArg(classId, objectId, subId,auxiliaryId, is_internal)
Definition: objectaccess.h:166
#define RowExclusiveLock
Definition: lockdefs.h:38
#define InvalidTransactionId
Definition: transam.h:31
#define MultiXactIdIsValid(multi)
Definition: multixact.h:27
void CatalogTupleUpdateWithInfo(Relation heapRel, ItemPointer otid, HeapTuple tup, CatalogIndexState indstate)
Definition: indexing.c:231
float float4
Definition: c.h:377
#define RELKIND_TOASTVALUE
Definition: pg_class.h:163
Oid RelationMapOidToFilenode(Oid relationId, bool shared)
Definition: relmapper.c:145
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
#define InvalidMultiXactId
Definition: multixact.h:23
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:1128
#define InvalidOid
Definition: postgres_ext.h:36
void RelationCloseSmgrByOid(Oid relationId)
Definition: relcache.c:2858
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define Assert(condition)
Definition: c.h:671
CatalogIndexState CatalogOpenIndexes(Relation heapRel)
Definition: indexing.c:40
FormData_pg_class * Form_pg_class
Definition: pg_class.h:95
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:158
#define AccessExclusiveLock
Definition: lockdefs.h:46
#define NameStr(name)
Definition: c.h:495
#define RELKIND_INDEX
Definition: pg_class.h:161
void CatalogCloseIndexes(CatalogIndexState indstate)
Definition: indexing.c:58
void CacheInvalidateRelcacheByTuple(HeapTuple classTuple)
Definition: inval.c:1261
#define elog
Definition: elog.h:219
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
void RelationMapUpdateMap(Oid relationId, Oid fileNode, bool shared, bool immediate)
Definition: relmapper.c:247