PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
cluster.c File Reference
#include "postgres.h"
#include "access/amapi.h"
#include "access/heapam.h"
#include "access/multixact.h"
#include "access/relscan.h"
#include "access/tableam.h"
#include "access/toast_internals.h"
#include "access/transam.h"
#include "access/xact.h"
#include "catalog/catalog.h"
#include "catalog/dependency.h"
#include "catalog/heap.h"
#include "catalog/index.h"
#include "catalog/namespace.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_am.h"
#include "catalog/pg_inherits.h"
#include "catalog/toasting.h"
#include "commands/cluster.h"
#include "commands/defrem.h"
#include "commands/progress.h"
#include "commands/tablecmds.h"
#include "commands/vacuum.h"
#include "miscadmin.h"
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.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 dependency graph for cluster.c:

Go to the source code of this file.

Data Structures

struct  RelToCluster
 

Functions

static void cluster_multiple_rels (List *rtcs, ClusterParams *params)
 
static void rebuild_relation (Relation OldHeap, Relation index, bool verbose)
 
static void copy_table_data (Relation NewHeap, Relation OldHeap, Relation OldIndex, bool verbose, bool *pSwapToastByContent, TransactionId *pFreezeXid, MultiXactId *pCutoffMulti)
 
static Listget_tables_to_cluster (MemoryContext cluster_context)
 
static Listget_tables_to_cluster_partitioned (MemoryContext cluster_context, Oid indexOid)
 
static bool cluster_is_permitted_for_relation (Oid relid, Oid userid)
 
void cluster (ParseState *pstate, ClusterStmt *stmt, bool isTopLevel)
 
void cluster_rel (Relation OldHeap, Oid indexOid, ClusterParams *params)
 
void check_index_is_clusterable (Relation OldHeap, Oid indexOid, 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)
 
static void swap_relation_files (Oid r1, Oid r2, bool target_is_pg_class, bool swap_toast_by_content, bool is_internal, TransactionId frozenXid, MultiXactId cutoffMulti, Oid *mapped_tables)
 
void finish_heap_swap (Oid OIDOldHeap, Oid OIDNewHeap, bool is_system_catalog, bool swap_toast_by_content, bool check_constraints, bool is_internal, TransactionId frozenXid, MultiXactId cutoffMulti, char newrelpersistence)
 

Function Documentation

◆ check_index_is_clusterable()

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

Definition at line 494 of file cluster.c.

495{
496 Relation OldIndex;
497
498 OldIndex = index_open(indexOid, lockmode);
499
500 /*
501 * Check that index is in fact an index on the given relation
502 */
503 if (OldIndex->rd_index == NULL ||
504 OldIndex->rd_index->indrelid != RelationGetRelid(OldHeap))
506 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
507 errmsg("\"%s\" is not an index for table \"%s\"",
508 RelationGetRelationName(OldIndex),
509 RelationGetRelationName(OldHeap))));
510
511 /* Index AM must allow clustering */
512 if (!OldIndex->rd_indam->amclusterable)
514 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
515 errmsg("cannot cluster on index \"%s\" because access method does not support clustering",
516 RelationGetRelationName(OldIndex))));
517
518 /*
519 * Disallow clustering on incomplete indexes (those that might not index
520 * every row of the relation). We could relax this by making a separate
521 * seqscan pass over the table to copy the missing rows, but that seems
522 * expensive and tedious.
523 */
524 if (!heap_attisnull(OldIndex->rd_indextuple, Anum_pg_index_indpred, NULL))
526 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
527 errmsg("cannot cluster on partial index \"%s\"",
528 RelationGetRelationName(OldIndex))));
529
530 /*
531 * Disallow if index is left over from a failed CREATE INDEX CONCURRENTLY;
532 * it might well not contain entries for every heap row, or might not even
533 * be internally consistent. (But note that we don't check indcheckxmin;
534 * the worst consequence of following broken HOT chains would be that we
535 * might put recently-dead tuples out-of-order in the new table, and there
536 * is little harm in that.)
537 */
538 if (!OldIndex->rd_index->indisvalid)
540 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
541 errmsg("cannot cluster on invalid index \"%s\"",
542 RelationGetRelationName(OldIndex))));
543
544 /* Drop relcache refcnt on OldIndex, but keep lock */
545 index_close(OldIndex, NoLock);
546}
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:456
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:177
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:133
#define NoLock
Definition: lockdefs.h:34
#define RelationGetRelid(relation)
Definition: rel.h:505
#define RelationGetRelationName(relation)
Definition: rel.h:539
bool amclusterable
Definition: amapi.h:253
struct IndexAmRoutine * rd_indam
Definition: rel.h:206
struct HeapTupleData * rd_indextuple
Definition: rel.h:194
Form_pg_index rd_index
Definition: rel.h:192

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(), cluster(), and cluster_rel().

◆ cluster()

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

Definition at line 107 of file cluster.c.

108{
109 ListCell *lc;
110 ClusterParams params = {0};
111 bool verbose = false;
112 Relation rel = NULL;
113 Oid indexOid = InvalidOid;
114 MemoryContext cluster_context;
115 List *rtcs;
116
117 /* Parse option list */
118 foreach(lc, stmt->params)
119 {
120 DefElem *opt = (DefElem *) lfirst(lc);
121
122 if (strcmp(opt->defname, "verbose") == 0)
123 verbose = defGetBoolean(opt);
124 else
126 (errcode(ERRCODE_SYNTAX_ERROR),
127 errmsg("unrecognized CLUSTER option \"%s\"",
128 opt->defname),
129 parser_errposition(pstate, opt->location)));
130 }
131
132 params.options = (verbose ? CLUOPT_VERBOSE : 0);
133
134 if (stmt->relation != NULL)
135 {
136 /* This is the single-relation case. */
137 Oid tableOid;
138
139 /*
140 * Find, lock, and check permissions on the table. We obtain
141 * AccessExclusiveLock right away to avoid lock-upgrade hazard in the
142 * single-transaction case.
143 */
144 tableOid = RangeVarGetRelidExtended(stmt->relation,
146 0,
148 NULL);
149 rel = table_open(tableOid, NoLock);
150
151 /*
152 * Reject clustering a remote temp table ... their local buffer
153 * manager is not going to cope.
154 */
155 if (RELATION_IS_OTHER_TEMP(rel))
157 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
158 errmsg("cannot cluster temporary tables of other sessions")));
159
160 if (stmt->indexname == NULL)
161 {
163
164 /* We need to find the index that has indisclustered set. */
165 foreach(index, RelationGetIndexList(rel))
166 {
167 indexOid = lfirst_oid(index);
168 if (get_index_isclustered(indexOid))
169 break;
170 indexOid = InvalidOid;
171 }
172
173 if (!OidIsValid(indexOid))
175 (errcode(ERRCODE_UNDEFINED_OBJECT),
176 errmsg("there is no previously clustered index for table \"%s\"",
177 stmt->relation->relname)));
178 }
179 else
180 {
181 /*
182 * The index is expected to be in the same namespace as the
183 * relation.
184 */
185 indexOid = get_relname_relid(stmt->indexname,
186 rel->rd_rel->relnamespace);
187 if (!OidIsValid(indexOid))
189 (errcode(ERRCODE_UNDEFINED_OBJECT),
190 errmsg("index \"%s\" for table \"%s\" does not exist",
191 stmt->indexname, stmt->relation->relname)));
192 }
193
194 /* For non-partitioned tables, do what we came here to do. */
195 if (rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
196 {
197 cluster_rel(rel, indexOid, &params);
198 /* cluster_rel closes the relation, but keeps lock */
199
200 return;
201 }
202 }
203
204 /*
205 * By here, we know we are in a multi-table situation. In order to avoid
206 * holding locks for too long, we want to process each table in its own
207 * transaction. This forces us to disallow running inside a user
208 * transaction block.
209 */
210 PreventInTransactionBlock(isTopLevel, "CLUSTER");
211
212 /* Also, we need a memory context to hold our list of relations */
213 cluster_context = AllocSetContextCreate(PortalContext,
214 "Cluster",
216
217 /*
218 * Either we're processing a partitioned table, or we were not given any
219 * table name at all. In either case, obtain a list of relations to
220 * process.
221 *
222 * In the former case, an index name must have been given, so we don't
223 * need to recheck its "indisclustered" bit, but we have to check that it
224 * is an index that we can cluster on. In the latter case, we set the
225 * option bit to have indisclustered verified.
226 *
227 * Rechecking the relation itself is necessary here in all cases.
228 */
229 params.options |= CLUOPT_RECHECK;
230 if (rel != NULL)
231 {
232 Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
234 rtcs = get_tables_to_cluster_partitioned(cluster_context, indexOid);
235
236 /* close relation, releasing lock on parent table */
238 }
239 else
240 {
241 rtcs = get_tables_to_cluster(cluster_context);
243 }
244
245 /* Do the job. */
246 cluster_multiple_rels(rtcs, &params);
247
248 /* Start a new transaction for the cleanup work. */
250
251 /* Clean up working storage */
252 MemoryContextDelete(cluster_context);
253}
#define Assert(condition)
Definition: c.h:815
#define OidIsValid(objectId)
Definition: c.h:732
void check_index_is_clusterable(Relation OldHeap, Oid indexOid, LOCKMODE lockmode)
Definition: cluster.c:494
static List * get_tables_to_cluster(MemoryContext cluster_context)
Definition: cluster.c:1638
void cluster_rel(Relation OldHeap, Oid indexOid, ClusterParams *params)
Definition: cluster.c:311
static List * get_tables_to_cluster_partitioned(MemoryContext cluster_context, Oid indexOid)
Definition: cluster.c:1692
static void cluster_multiple_rels(List *rtcs, ClusterParams *params)
Definition: cluster.c:263
#define CLUOPT_VERBOSE
Definition: cluster.h:23
#define CLUOPT_RECHECK_ISCLUSTERED
Definition: cluster.h:25
#define CLUOPT_RECHECK
Definition: cluster.h:24
bool defGetBoolean(DefElem *def)
Definition: define.c:94
#define stmt
Definition: indent_codes.h:59
int verbose
#define AccessExclusiveLock
Definition: lockdefs.h:43
#define AccessShareLock
Definition: lockdefs.h:36
bool get_index_isclustered(Oid index_oid)
Definition: lsyscache.c:3601
Oid get_relname_relid(const char *relname, Oid relnamespace)
Definition: lsyscache.c:1885
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:454
MemoryContext PortalContext
Definition: mcxt.c:158
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
Oid RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, uint32 flags, RangeVarGetRelidCallback callback, void *callback_arg)
Definition: namespace.c:441
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:106
#define lfirst(lc)
Definition: pg_list.h:172
#define lfirst_oid(lc)
Definition: pg_list.h:174
#define InvalidOid
Definition: postgres_ext.h:37
unsigned int Oid
Definition: postgres_ext.h:32
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:658
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4756
bits32 options
Definition: cluster.h:30
char * defname
Definition: parsenodes.h:826
ParseLoc location
Definition: parsenodes.h:830
Definition: pg_list.h:54
Form_pg_class rd_rel
Definition: rel.h:111
Definition: type.h:96
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40
void RangeVarCallbackMaintainsTable(const RangeVar *relation, Oid relId, Oid oldRelId, void *arg)
Definition: tablecmds.c:18404
void PreventInTransactionBlock(bool isTopLevel, const char *stmtType)
Definition: xact.c:3640
void StartTransactionCommand(void)
Definition: xact.c:3051

References AccessExclusiveLock, AccessShareLock, ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, check_index_is_clusterable(), CLUOPT_RECHECK, CLUOPT_RECHECK_ISCLUSTERED, CLUOPT_VERBOSE, cluster_multiple_rels(), cluster_rel(), defGetBoolean(), DefElem::defname, ereport, errcode(), errmsg(), ERROR, get_index_isclustered(), get_relname_relid(), get_tables_to_cluster(), get_tables_to_cluster_partitioned(), InvalidOid, lfirst, lfirst_oid, DefElem::location, MemoryContextDelete(), NoLock, OidIsValid, ClusterParams::options, parser_errposition(), PortalContext, PreventInTransactionBlock(), RangeVarCallbackMaintainsTable(), RangeVarGetRelidExtended(), RelationData::rd_rel, RELATION_IS_OTHER_TEMP, RelationGetIndexList(), StartTransactionCommand(), stmt, table_close(), table_open(), and verbose.

Referenced by adjust_data_dir(), check_bin_dir(), check_data_dir(), check_for_connection_status(), check_for_data_types_usage(), check_for_incompatible_polymorphics(), check_for_isn_and_int8_passing_mismatch(), check_for_pg_role_prefix(), check_for_prepared_transactions(), check_for_tables_with_oids(), check_for_user_defined_encoding_conversions(), check_for_user_defined_postfix_ops(), check_is_install_user(), cluster_conn_opts(), connectToServer(), get_bin_version(), get_control_data(), get_db_conn(), get_db_infos(), get_db_rel_and_slot_infos(), get_major_server_version(), get_sock_dir(), get_subscription_count(), get_template0_info(), jsonb_9_4_check_applicable(), old_9_6_invalidate_hash_indexes(), process_query_result(), process_slot(), report_extension_updates(), set_tablespace_directory_suffix(), standard_ProcessUtility(), start_conn(), start_postmaster(), stop_postmaster(), and upgrade_task_run().

◆ cluster_is_permitted_for_relation()

static bool cluster_is_permitted_for_relation ( Oid  relid,
Oid  userid 
)
static

Definition at line 1740 of file cluster.c.

1741{
1742 if (pg_class_aclcheck(relid, userid, ACL_MAINTAIN) == ACLCHECK_OK)
1743 return true;
1744
1746 (errmsg("permission denied to cluster \"%s\", skipping it",
1747 get_rel_name(relid))));
1748 return false;
1749}
@ ACLCHECK_OK
Definition: acl.h:183
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4007
#define WARNING
Definition: elog.h:36
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1928
#define ACL_MAINTAIN
Definition: parsenodes.h:90

References ACL_MAINTAIN, ACLCHECK_OK, ereport, errmsg(), get_rel_name(), pg_class_aclcheck(), and WARNING.

Referenced by cluster_rel(), get_tables_to_cluster(), and get_tables_to_cluster_partitioned().

◆ cluster_multiple_rels()

static void cluster_multiple_rels ( List rtcs,
ClusterParams params 
)
static

Definition at line 263 of file cluster.c.

264{
265 ListCell *lc;
266
267 /* Commit to get out of starting transaction */
270
271 /* Cluster the tables, each in a separate transaction */
272 foreach(lc, rtcs)
273 {
274 RelToCluster *rtc = (RelToCluster *) lfirst(lc);
275 Relation rel;
276
277 /* Start a new transaction for each relation. */
279
280 /* functions in indexes may want a snapshot set */
282
284
285 /* Process this table */
286 cluster_rel(rel, rtc->indexOid, params);
287 /* cluster_rel closes the relation, but keeps lock */
288
291 }
292}
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:212
void PushActiveSnapshot(Snapshot snapshot)
Definition: snapmgr.c:610
void PopActiveSnapshot(void)
Definition: snapmgr.c:703
Oid indexOid
Definition: cluster.c:67
Oid tableOid
Definition: cluster.c:66
void CommitTransactionCommand(void)
Definition: xact.c:3149

References AccessExclusiveLock, cluster_rel(), CommitTransactionCommand(), GetTransactionSnapshot(), RelToCluster::indexOid, lfirst, PopActiveSnapshot(), PushActiveSnapshot(), StartTransactionCommand(), table_open(), and RelToCluster::tableOid.

Referenced by cluster().

◆ cluster_rel()

void cluster_rel ( Relation  OldHeap,
Oid  indexOid,
ClusterParams params 
)

Definition at line 311 of file cluster.c.

312{
313 Oid tableOid = RelationGetRelid(OldHeap);
314 Oid save_userid;
315 int save_sec_context;
316 int save_nestlevel;
317 bool verbose = ((params->options & CLUOPT_VERBOSE) != 0);
318 bool recheck = ((params->options & CLUOPT_RECHECK) != 0);
320
322
323 /* Check for user-requested abort. */
325
327 if (OidIsValid(indexOid))
330 else
333
334 /*
335 * Switch to the table owner's userid, so that any index functions are run
336 * as that user. Also lock down security-restricted operations and
337 * arrange to make GUC variable changes local to this command.
338 */
339 GetUserIdAndSecContext(&save_userid, &save_sec_context);
340 SetUserIdAndSecContext(OldHeap->rd_rel->relowner,
341 save_sec_context | SECURITY_RESTRICTED_OPERATION);
342 save_nestlevel = NewGUCNestLevel();
344
345 /*
346 * Since we may open a new transaction for each relation, we have to check
347 * that the relation still is what we think it is.
348 *
349 * If this is a single-transaction CLUSTER, we can skip these tests. We
350 * *must* skip the one on indisclustered since it would reject an attempt
351 * to cluster a not-previously-clustered index.
352 */
353 if (recheck)
354 {
355 /* Check that the user still has privileges for the relation */
356 if (!cluster_is_permitted_for_relation(tableOid, save_userid))
357 {
359 goto out;
360 }
361
362 /*
363 * Silently skip a temp table for a remote session. Only doing this
364 * check in the "recheck" case is appropriate (which currently means
365 * somebody is executing a database-wide CLUSTER or on a partitioned
366 * table), because there is another check in cluster() which will stop
367 * any attempt to cluster remote temp tables by name. There is
368 * another check in cluster_rel which is redundant, but we leave it
369 * for extra safety.
370 */
371 if (RELATION_IS_OTHER_TEMP(OldHeap))
372 {
374 goto out;
375 }
376
377 if (OidIsValid(indexOid))
378 {
379 /*
380 * Check that the index still exists
381 */
382 if (!SearchSysCacheExists1(RELOID, ObjectIdGetDatum(indexOid)))
383 {
385 goto out;
386 }
387
388 /*
389 * Check that the index is still the one with indisclustered set,
390 * if needed.
391 */
392 if ((params->options & CLUOPT_RECHECK_ISCLUSTERED) != 0 &&
393 !get_index_isclustered(indexOid))
394 {
396 goto out;
397 }
398 }
399 }
400
401 /*
402 * We allow VACUUM FULL, but not CLUSTER, on shared catalogs. CLUSTER
403 * would work in most respects, but the index would only get marked as
404 * indisclustered in the current database, leading to unexpected behavior
405 * if CLUSTER were later invoked in another database.
406 */
407 if (OidIsValid(indexOid) && OldHeap->rd_rel->relisshared)
409 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
410 errmsg("cannot cluster a shared catalog")));
411
412 /*
413 * Don't process temp tables of other backends ... their local buffer
414 * manager is not going to cope.
415 */
416 if (RELATION_IS_OTHER_TEMP(OldHeap))
417 {
418 if (OidIsValid(indexOid))
420 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
421 errmsg("cannot cluster temporary tables of other sessions")));
422 else
424 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
425 errmsg("cannot vacuum temporary tables of other sessions")));
426 }
427
428 /*
429 * Also check for active uses of the relation in the current transaction,
430 * including open scans and pending AFTER trigger events.
431 */
432 CheckTableNotInUse(OldHeap, OidIsValid(indexOid) ? "CLUSTER" : "VACUUM");
433
434 /* Check heap and index are valid to cluster on */
435 if (OidIsValid(indexOid))
436 {
437 /* verify the index is good and lock it */
439 /* also open it */
440 index = index_open(indexOid, NoLock);
441 }
442 else
443 index = NULL;
444
445 /*
446 * Quietly ignore the request if this is a materialized view which has not
447 * been populated from its query. No harm is done because there is no data
448 * to deal with, and we don't want to throw an error if this is part of a
449 * multi-relation request -- for example, CLUSTER was run on the entire
450 * database.
451 */
452 if (OldHeap->rd_rel->relkind == RELKIND_MATVIEW &&
453 !RelationIsPopulated(OldHeap))
454 {
456 goto out;
457 }
458
459 Assert(OldHeap->rd_rel->relkind == RELKIND_RELATION ||
460 OldHeap->rd_rel->relkind == RELKIND_MATVIEW ||
461 OldHeap->rd_rel->relkind == RELKIND_TOASTVALUE);
462
463 /*
464 * All predicate locks on the tuples or pages are about to be made
465 * invalid, because we move tuples around. Promote them to relation
466 * locks. Predicate locks on indexes will be promoted when they are
467 * reindexed.
468 */
470
471 /* rebuild_relation does all the dirty work */
472 rebuild_relation(OldHeap, index, verbose);
473 /* rebuild_relation closes OldHeap, and index if valid */
474
475out:
476 /* Roll back any GUC changes executed by index functions */
477 AtEOXact_GUC(false, save_nestlevel);
478
479 /* Restore userid and security context */
480 SetUserIdAndSecContext(save_userid, save_sec_context);
481
483}
void pgstat_progress_start_command(ProgressCommandType cmdtype, Oid relid)
void pgstat_progress_update_param(int index, int64 val)
void pgstat_progress_end_command(void)
@ PROGRESS_COMMAND_CLUSTER
static bool cluster_is_permitted_for_relation(Oid relid, Oid userid)
Definition: cluster.c:1740
static void rebuild_relation(Relation OldHeap, Relation index, bool verbose)
Definition: cluster.c:629
int NewGUCNestLevel(void)
Definition: guc.c:2235
void RestrictSearchPath(void)
Definition: guc.c:2246
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:2262
bool CheckRelationLockedByMe(Relation relation, LOCKMODE lockmode, bool orstronger)
Definition: lmgr.c:329
#define SECURITY_RESTRICTED_OPERATION
Definition: miscadmin.h:318
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
Definition: miscinit.c:660
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:667
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
void TransferPredicateLocksToHeapRelation(Relation relation)
Definition: predicate.c:3113
#define PROGRESS_CLUSTER_COMMAND_VACUUM_FULL
Definition: progress.h:78
#define PROGRESS_CLUSTER_COMMAND_CLUSTER
Definition: progress.h:77
#define PROGRESS_CLUSTER_COMMAND
Definition: progress.h:58
#define RelationIsPopulated(relation)
Definition: rel.h:677
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:205
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:100
void CheckTableNotInUse(Relation rel, const char *stmt)
Definition: tablecmds.c:4360

References AccessExclusiveLock, Assert, AtEOXact_GUC(), CHECK_FOR_INTERRUPTS, check_index_is_clusterable(), CheckRelationLockedByMe(), CheckTableNotInUse(), CLUOPT_RECHECK, CLUOPT_RECHECK_ISCLUSTERED, CLUOPT_VERBOSE, cluster_is_permitted_for_relation(), ereport, errcode(), errmsg(), ERROR, get_index_isclustered(), GetUserIdAndSecContext(), index_open(), NewGUCNestLevel(), NoLock, ObjectIdGetDatum(), OidIsValid, ClusterParams::options, 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, RelationGetRelid, RelationIsPopulated, RestrictSearchPath(), SearchSysCacheExists1, SECURITY_RESTRICTED_OPERATION, SetUserIdAndSecContext(), TransferPredicateLocksToHeapRelation(), and verbose.

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

◆ copy_table_data()

static void copy_table_data ( Relation  NewHeap,
Relation  OldHeap,
Relation  OldIndex,
bool  verbose,
bool *  pSwapToastByContent,
TransactionId pFreezeXid,
MultiXactId pCutoffMulti 
)
static

Definition at line 831 of file cluster.c.

834{
835 Relation relRelation;
836 HeapTuple reltup;
837 Form_pg_class relform;
840 VacuumParams params;
841 struct VacuumCutoffs cutoffs;
842 bool use_sort;
843 double num_tuples = 0,
844 tups_vacuumed = 0,
845 tups_recently_dead = 0;
846 BlockNumber num_pages;
847 int elevel = verbose ? INFO : DEBUG2;
848 PGRUsage ru0;
849 char *nspname;
850
851 pg_rusage_init(&ru0);
852
853 /* Store a copy of the namespace name for logging purposes */
854 nspname = get_namespace_name(RelationGetNamespace(OldHeap));
855
856 /*
857 * Their tuple descriptors should be exactly alike, but here we only need
858 * assume that they have the same number of columns.
859 */
860 oldTupDesc = RelationGetDescr(OldHeap);
861 newTupDesc = RelationGetDescr(NewHeap);
862 Assert(newTupDesc->natts == oldTupDesc->natts);
863
864 /*
865 * If the OldHeap has a toast table, get lock on the toast table to keep
866 * it from being vacuumed. This is needed because autovacuum processes
867 * toast tables independently of their main tables, with no lock on the
868 * latter. If an autovacuum were to start on the toast table after we
869 * compute our OldestXmin below, it would use a later OldestXmin, and then
870 * possibly remove as DEAD toast tuples belonging to main tuples we think
871 * are only RECENTLY_DEAD. Then we'd fail while trying to copy those
872 * tuples.
873 *
874 * We don't need to open the toast relation here, just lock it. The lock
875 * will be held till end of transaction.
876 */
877 if (OldHeap->rd_rel->reltoastrelid)
878 LockRelationOid(OldHeap->rd_rel->reltoastrelid, AccessExclusiveLock);
879
880 /*
881 * If both tables have TOAST tables, perform toast swap by content. It is
882 * possible that the old table has a toast table but the new one doesn't,
883 * if toastable columns have been dropped. In that case we have to do
884 * swap by links. This is okay because swap by content is only essential
885 * for system catalogs, and we don't support schema changes for them.
886 */
887 if (OldHeap->rd_rel->reltoastrelid && NewHeap->rd_rel->reltoastrelid)
888 {
889 *pSwapToastByContent = true;
890
891 /*
892 * When doing swap by content, any toast pointers written into NewHeap
893 * must use the old toast table's OID, because that's where the toast
894 * data will eventually be found. Set this up by setting rd_toastoid.
895 * This also tells toast_save_datum() to preserve the toast value
896 * OIDs, which we want so as not to invalidate toast pointers in
897 * system catalog caches, and to avoid making multiple copies of a
898 * single toast value.
899 *
900 * Note that we must hold NewHeap open until we are done writing data,
901 * since the relcache will not guarantee to remember this setting once
902 * the relation is closed. Also, this technique depends on the fact
903 * that no one will try to read from the NewHeap until after we've
904 * finished writing it and swapping the rels --- otherwise they could
905 * follow the toast pointers to the wrong place. (It would actually
906 * work for values copied over from the old toast table, but not for
907 * any values that we toast which were previously not toasted.)
908 */
909 NewHeap->rd_toastoid = OldHeap->rd_rel->reltoastrelid;
910 }
911 else
912 *pSwapToastByContent = false;
913
914 /*
915 * Compute xids used to freeze and weed out dead tuples and multixacts.
916 * Since we're going to rewrite the whole table anyway, there's no reason
917 * not to be aggressive about this.
918 */
919 memset(&params, 0, sizeof(VacuumParams));
920 vacuum_get_cutoffs(OldHeap, &params, &cutoffs);
921
922 /*
923 * FreezeXid will become the table's new relfrozenxid, and that mustn't go
924 * backwards, so take the max.
925 */
926 {
927 TransactionId relfrozenxid = OldHeap->rd_rel->relfrozenxid;
928
930 TransactionIdPrecedes(cutoffs.FreezeLimit, relfrozenxid))
931 cutoffs.FreezeLimit = relfrozenxid;
932 }
933
934 /*
935 * MultiXactCutoff, similarly, shouldn't go backwards either.
936 */
937 {
938 MultiXactId relminmxid = OldHeap->rd_rel->relminmxid;
939
941 MultiXactIdPrecedes(cutoffs.MultiXactCutoff, relminmxid))
942 cutoffs.MultiXactCutoff = relminmxid;
943 }
944
945 /*
946 * Decide whether to use an indexscan or seqscan-and-optional-sort to scan
947 * the OldHeap. We know how to use a sort to duplicate the ordering of a
948 * btree index, and will use seqscan-and-sort for that case if the planner
949 * tells us it's cheaper. Otherwise, always indexscan if an index is
950 * provided, else plain seqscan.
951 */
952 if (OldIndex != NULL && OldIndex->rd_rel->relam == BTREE_AM_OID)
953 use_sort = plan_cluster_use_sort(RelationGetRelid(OldHeap),
954 RelationGetRelid(OldIndex));
955 else
956 use_sort = false;
957
958 /* Log what we're doing */
959 if (OldIndex != NULL && !use_sort)
960 ereport(elevel,
961 (errmsg("clustering \"%s.%s\" using index scan on \"%s\"",
962 nspname,
964 RelationGetRelationName(OldIndex))));
965 else if (use_sort)
966 ereport(elevel,
967 (errmsg("clustering \"%s.%s\" using sequential scan and sort",
968 nspname,
969 RelationGetRelationName(OldHeap))));
970 else
971 ereport(elevel,
972 (errmsg("vacuuming \"%s.%s\"",
973 nspname,
974 RelationGetRelationName(OldHeap))));
975
976 /*
977 * Hand off the actual copying to AM specific function, the generic code
978 * cannot know how to deal with visibility across AMs. Note that this
979 * routine is allowed to set FreezeXid / MultiXactCutoff to different
980 * values (e.g. because the AM doesn't use freezing).
981 */
982 table_relation_copy_for_cluster(OldHeap, NewHeap, OldIndex, use_sort,
983 cutoffs.OldestXmin, &cutoffs.FreezeLimit,
984 &cutoffs.MultiXactCutoff,
985 &num_tuples, &tups_vacuumed,
986 &tups_recently_dead);
987
988 /* return selected values to caller, get set as relfrozenxid/minmxid */
989 *pFreezeXid = cutoffs.FreezeLimit;
990 *pCutoffMulti = cutoffs.MultiXactCutoff;
991
992 /* Reset rd_toastoid just to be tidy --- it shouldn't be looked at again */
993 NewHeap->rd_toastoid = InvalidOid;
994
995 num_pages = RelationGetNumberOfBlocks(NewHeap);
996
997 /* Log what we did */
998 ereport(elevel,
999 (errmsg("\"%s.%s\": found %.0f removable, %.0f nonremovable row versions in %u pages",
1000 nspname,
1001 RelationGetRelationName(OldHeap),
1002 tups_vacuumed, num_tuples,
1003 RelationGetNumberOfBlocks(OldHeap)),
1004 errdetail("%.0f dead row versions cannot be removed yet.\n"
1005 "%s.",
1006 tups_recently_dead,
1007 pg_rusage_show(&ru0))));
1008
1009 /* Update pg_class to reflect the correct values of pages and tuples. */
1010 relRelation = table_open(RelationRelationId, RowExclusiveLock);
1011
1012 reltup = SearchSysCacheCopy1(RELOID,
1014 if (!HeapTupleIsValid(reltup))
1015 elog(ERROR, "cache lookup failed for relation %u",
1016 RelationGetRelid(NewHeap));
1017 relform = (Form_pg_class) GETSTRUCT(reltup);
1018
1019 relform->relpages = num_pages;
1020 relform->reltuples = num_tuples;
1021
1022 /* Don't update the stats for pg_class. See swap_relation_files. */
1023 if (RelationGetRelid(OldHeap) != RelationRelationId)
1024 CatalogTupleUpdate(relRelation, &reltup->t_self, reltup);
1025 else
1027
1028 /* Clean up. */
1029 heap_freetuple(reltup);
1030 table_close(relRelation, RowExclusiveLock);
1031
1032 /* Make the update visible */
1034}
uint32 BlockNumber
Definition: block.h:31
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:273
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:204
TransactionId MultiXactId
Definition: c.h:619
uint32 TransactionId
Definition: c.h:609
int errdetail(const char *fmt,...)
Definition: elog.c:1203
#define DEBUG2
Definition: elog.h:29
#define elog(elevel,...)
Definition: elog.h:225
#define INFO
Definition: elog.h:34
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1435
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:313
void CacheInvalidateRelcacheByTuple(HeapTuple classTuple)
Definition: inval.c:1587
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:107
#define RowExclusiveLock
Definition: lockdefs.h:38
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3366
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3317
#define MultiXactIdIsValid(multi)
Definition: multixact.h:28
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
const char * pg_rusage_show(const PGRUsage *ru0)
Definition: pg_rusage.c:40
void pg_rusage_init(PGRUsage *ru0)
Definition: pg_rusage.c:27
bool plan_cluster_use_sort(Oid tableOid, Oid indexOid)
Definition: planner.c:6650
#define RelationGetDescr(relation)
Definition: rel.h:531
#define RelationGetNamespace(relation)
Definition: rel.h:546
ItemPointerData t_self
Definition: htup.h:65
Oid rd_toastoid
Definition: rel.h:251
TransactionId relfrozenxid
Definition: vacuum.h:251
MultiXactId relminmxid
Definition: vacuum.h:252
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:91
static void table_relation_copy_for_cluster(Relation OldTable, Relation NewTable, Relation OldIndex, bool use_sort, TransactionId OldestXmin, TransactionId *xid_cutoff, MultiXactId *multi_cutoff, double *num_tuples, double *tups_vacuumed, double *tups_recently_dead)
Definition: tableam.h:1683
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:280
#define TransactionIdIsValid(xid)
Definition: transam.h:41
bool vacuum_get_cutoffs(Relation rel, const VacuumParams *params, struct VacuumCutoffs *cutoffs)
Definition: vacuum.c:1084
void CommandCounterIncrement(void)
Definition: xact.c:1099

References AccessExclusiveLock, Assert, CacheInvalidateRelcacheByTuple(), CatalogTupleUpdate(), CommandCounterIncrement(), DEBUG2, elog, ereport, errdetail(), errmsg(), ERROR, VacuumCutoffs::FreezeLimit, get_namespace_name(), GETSTRUCT, heap_freetuple(), HeapTupleIsValid, INFO, InvalidOid, LockRelationOid(), VacuumCutoffs::MultiXactCutoff, MultiXactIdIsValid, MultiXactIdPrecedes(), ObjectIdGetDatum(), VacuumCutoffs::OldestXmin, pg_rusage_init(), pg_rusage_show(), PG_USED_FOR_ASSERTS_ONLY, plan_cluster_use_sort(), RelationData::rd_rel, RelationData::rd_toastoid, RelationGetDescr, RelationGetNamespace, RelationGetNumberOfBlocks, RelationGetRelationName, RelationGetRelid, VacuumCutoffs::relfrozenxid, VacuumCutoffs::relminmxid, RowExclusiveLock, SearchSysCacheCopy1, HeapTupleData::t_self, table_close(), table_open(), table_relation_copy_for_cluster(), TransactionIdIsValid, TransactionIdPrecedes(), vacuum_get_cutoffs(), and verbose.

Referenced by rebuild_relation().

◆ finish_heap_swap()

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

Definition at line 1440 of file cluster.c.

1448{
1449 ObjectAddress object;
1450 Oid mapped_tables[4];
1451 int reindex_flags;
1452 ReindexParams reindex_params = {0};
1453 int i;
1454
1455 /* Report that we are now swapping relation files */
1458
1459 /* Zero out possible results from swapped_relation_files */
1460 memset(mapped_tables, 0, sizeof(mapped_tables));
1461
1462 /*
1463 * Swap the contents of the heap relations (including any toast tables).
1464 * Also set old heap's relfrozenxid to frozenXid.
1465 */
1466 swap_relation_files(OIDOldHeap, OIDNewHeap,
1467 (OIDOldHeap == RelationRelationId),
1468 swap_toast_by_content, is_internal,
1469 frozenXid, cutoffMulti, mapped_tables);
1470
1471 /*
1472 * If it's a system catalog, queue a sinval message to flush all catcaches
1473 * on the catalog when we reach CommandCounterIncrement.
1474 */
1475 if (is_system_catalog)
1476 CacheInvalidateCatalog(OIDOldHeap);
1477
1478 /*
1479 * Rebuild each index on the relation (but not the toast table, which is
1480 * all-new at this point). It is important to do this before the DROP
1481 * step because if we are processing a system catalog that will be used
1482 * during DROP, we want to have its indexes available. There is no
1483 * advantage to the other order anyway because this is all transactional,
1484 * so no chance to reclaim disk space before commit. We do not need a
1485 * final CommandCounterIncrement() because reindex_relation does it.
1486 *
1487 * Note: because index_build is called via reindex_relation, it will never
1488 * set indcheckxmin true for the indexes. This is OK even though in some
1489 * sense we are building new indexes rather than rebuilding existing ones,
1490 * because the new heap won't contain any HOT chains at all, let alone
1491 * broken ones, so it can't be necessary to set indcheckxmin.
1492 */
1493 reindex_flags = REINDEX_REL_SUPPRESS_INDEX_USE;
1494 if (check_constraints)
1495 reindex_flags |= REINDEX_REL_CHECK_CONSTRAINTS;
1496
1497 /*
1498 * Ensure that the indexes have the same persistence as the parent
1499 * relation.
1500 */
1501 if (newrelpersistence == RELPERSISTENCE_UNLOGGED)
1502 reindex_flags |= REINDEX_REL_FORCE_INDEXES_UNLOGGED;
1503 else if (newrelpersistence == RELPERSISTENCE_PERMANENT)
1504 reindex_flags |= REINDEX_REL_FORCE_INDEXES_PERMANENT;
1505
1506 /* Report that we are now reindexing relations */
1509
1510 reindex_relation(NULL, OIDOldHeap, reindex_flags, &reindex_params);
1511
1512 /* Report that we are now doing clean up */
1515
1516 /*
1517 * If the relation being rebuilt is pg_class, swap_relation_files()
1518 * couldn't update pg_class's own pg_class entry (check comments in
1519 * swap_relation_files()), thus relfrozenxid was not updated. That's
1520 * annoying because a potential reason for doing a VACUUM FULL is a
1521 * imminent or actual anti-wraparound shutdown. So, now that we can
1522 * access the new relation using its indices, update relfrozenxid.
1523 * pg_class doesn't have a toast relation, so we don't need to update the
1524 * corresponding toast relation. Not that there's little point moving all
1525 * relfrozenxid updates here since swap_relation_files() needs to write to
1526 * pg_class for non-mapped relations anyway.
1527 */
1528 if (OIDOldHeap == RelationRelationId)
1529 {
1530 Relation relRelation;
1531 HeapTuple reltup;
1532 Form_pg_class relform;
1533
1534 relRelation = table_open(RelationRelationId, RowExclusiveLock);
1535
1536 reltup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(OIDOldHeap));
1537 if (!HeapTupleIsValid(reltup))
1538 elog(ERROR, "cache lookup failed for relation %u", OIDOldHeap);
1539 relform = (Form_pg_class) GETSTRUCT(reltup);
1540
1541 relform->relfrozenxid = frozenXid;
1542 relform->relminmxid = cutoffMulti;
1543
1544 CatalogTupleUpdate(relRelation, &reltup->t_self, reltup);
1545
1546 table_close(relRelation, RowExclusiveLock);
1547 }
1548
1549 /* Destroy new heap with old filenumber */
1550 object.classId = RelationRelationId;
1551 object.objectId = OIDNewHeap;
1552 object.objectSubId = 0;
1553
1554 /*
1555 * The new relation is local to our transaction and we know nothing
1556 * depends on it, so DROP_RESTRICT should be OK.
1557 */
1559
1560 /* performDeletion does CommandCounterIncrement at end */
1561
1562 /*
1563 * Now we must remove any relation mapping entries that we set up for the
1564 * transient table, as well as its toast table and toast index if any. If
1565 * we fail to do this before commit, the relmapper will complain about new
1566 * permanent map entries being added post-bootstrap.
1567 */
1568 for (i = 0; OidIsValid(mapped_tables[i]); i++)
1569 RelationMapRemoveMapping(mapped_tables[i]);
1570
1571 /*
1572 * At this point, everything is kosher except that, if we did toast swap
1573 * by links, the toast table's name corresponds to the transient table.
1574 * The name is irrelevant to the backend because it's referenced by OID,
1575 * but users looking at the catalogs could be confused. Rename it to
1576 * prevent this problem.
1577 *
1578 * Note no lock required on the relation, because we already hold an
1579 * exclusive lock on it.
1580 */
1581 if (!swap_toast_by_content)
1582 {
1583 Relation newrel;
1584
1585 newrel = table_open(OIDOldHeap, NoLock);
1586 if (OidIsValid(newrel->rd_rel->reltoastrelid))
1587 {
1588 Oid toastidx;
1589 char NewToastName[NAMEDATALEN];
1590
1591 /* Get the associated valid index to be renamed */
1592 toastidx = toast_get_valid_index(newrel->rd_rel->reltoastrelid,
1593 NoLock);
1594
1595 /* rename the toast table ... */
1596 snprintf(NewToastName, NAMEDATALEN, "pg_toast_%u",
1597 OIDOldHeap);
1598 RenameRelationInternal(newrel->rd_rel->reltoastrelid,
1599 NewToastName, true, false);
1600
1601 /* ... and its valid index too. */
1602 snprintf(NewToastName, NAMEDATALEN, "pg_toast_%u_index",
1603 OIDOldHeap);
1604
1605 RenameRelationInternal(toastidx,
1606 NewToastName, true, true);
1607
1608 /*
1609 * Reset the relrewrite for the toast. The command-counter
1610 * increment is required here as we are about to update the tuple
1611 * that is updated as part of RenameRelationInternal.
1612 */
1614 ResetRelRewrite(newrel->rd_rel->reltoastrelid);
1615 }
1616 relation_close(newrel, NoLock);
1617 }
1618
1619 /* if it's not a catalog table, clear any missing attribute settings */
1620 if (!is_system_catalog)
1621 {
1622 Relation newrel;
1623
1624 newrel = table_open(OIDOldHeap, NoLock);
1625 RelationClearMissing(newrel);
1626 relation_close(newrel, NoLock);
1627 }
1628}
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:1063
void performDeletion(const ObjectAddress *object, DropBehavior behavior, int flags)
Definition: dependency.c:273
#define PERFORM_DELETION_INTERNAL
Definition: dependency.h:92
void RelationClearMissing(Relation rel)
Definition: heap.c:1940
bool reindex_relation(const ReindexStmt *stmt, Oid relid, int flags, const ReindexParams *params)
Definition: index.c:3919
#define REINDEX_REL_FORCE_INDEXES_UNLOGGED
Definition: index.h:162
#define REINDEX_REL_SUPPRESS_INDEX_USE
Definition: index.h:160
#define REINDEX_REL_FORCE_INDEXES_PERMANENT
Definition: index.h:163
#define REINDEX_REL_CHECK_CONSTRAINTS
Definition: index.h:161
void CacheInvalidateCatalog(Oid catalogId)
Definition: inval.c:1530
int i
Definition: isn.c:72
@ DROP_RESTRICT
Definition: parsenodes.h:2385
#define NAMEDATALEN
#define snprintf
Definition: port.h:238
#define PROGRESS_CLUSTER_PHASE
Definition: progress.h:59
#define PROGRESS_CLUSTER_PHASE_REBUILD_INDEX
Definition: progress.h:73
#define PROGRESS_CLUSTER_PHASE_FINAL_CLEANUP
Definition: progress.h:74
#define PROGRESS_CLUSTER_PHASE_SWAP_REL_FILES
Definition: progress.h:72
void RelationMapRemoveMapping(Oid relationId)
Definition: relmapper.c:438
void ResetRelRewrite(Oid myrelid)
Definition: tablecmds.c:4307
void RenameRelationInternal(Oid myrelid, const char *newrelname, bool is_internal, bool is_index)
Definition: tablecmds.c:4214
Oid toast_get_valid_index(Oid toastoid, LOCKMODE lock)

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(), 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().

◆ get_tables_to_cluster()

static List * get_tables_to_cluster ( MemoryContext  cluster_context)
static

Definition at line 1638 of file cluster.c.

1639{
1640 Relation indRelation;
1641 TableScanDesc scan;
1642 ScanKeyData entry;
1643 HeapTuple indexTuple;
1645 MemoryContext old_context;
1646 List *rtcs = NIL;
1647
1648 /*
1649 * Get all indexes that have indisclustered set and that the current user
1650 * has the appropriate privileges for.
1651 */
1652 indRelation = table_open(IndexRelationId, AccessShareLock);
1653 ScanKeyInit(&entry,
1654 Anum_pg_index_indisclustered,
1655 BTEqualStrategyNumber, F_BOOLEQ,
1656 BoolGetDatum(true));
1657 scan = table_beginscan_catalog(indRelation, 1, &entry);
1658 while ((indexTuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1659 {
1660 RelToCluster *rtc;
1661
1662 index = (Form_pg_index) GETSTRUCT(indexTuple);
1663
1665 continue;
1666
1667 /* Use a permanent memory context for the result list */
1668 old_context = MemoryContextSwitchTo(cluster_context);
1669
1670 rtc = (RelToCluster *) palloc(sizeof(RelToCluster));
1671 rtc->tableOid = index->indrelid;
1672 rtc->indexOid = index->indexrelid;
1673 rtcs = lappend(rtcs, rtc);
1674
1675 MemoryContextSwitchTo(old_context);
1676 }
1677 table_endscan(scan);
1678
1679 relation_close(indRelation, AccessShareLock);
1680
1681 return rtcs;
1682}
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1264
List * lappend(List *list, void *datum)
Definition: list.c:339
void * palloc(Size size)
Definition: mcxt.c:1317
Oid GetUserId(void)
Definition: miscinit.c:517
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
FormData_pg_index * Form_pg_index
Definition: pg_index.h:70
#define NIL
Definition: pg_list.h:68
static Datum BoolGetDatum(bool X)
Definition: postgres.h:107
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
@ ForwardScanDirection
Definition: sdir.h:28
#define BTEqualStrategyNumber
Definition: stratnum.h:31
TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, struct ScanKeyData *key)
Definition: tableam.c:112
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:1024

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

Referenced by cluster().

◆ get_tables_to_cluster_partitioned()

static List * get_tables_to_cluster_partitioned ( MemoryContext  cluster_context,
Oid  indexOid 
)
static

Definition at line 1692 of file cluster.c.

1693{
1694 List *inhoids;
1695 ListCell *lc;
1696 List *rtcs = NIL;
1697 MemoryContext old_context;
1698
1699 /* Do not lock the children until they're processed */
1700 inhoids = find_all_inheritors(indexOid, NoLock, NULL);
1701
1702 foreach(lc, inhoids)
1703 {
1704 Oid indexrelid = lfirst_oid(lc);
1705 Oid relid = IndexGetRelation(indexrelid, false);
1706 RelToCluster *rtc;
1707
1708 /* consider only leaf indexes */
1709 if (get_rel_relkind(indexrelid) != RELKIND_INDEX)
1710 continue;
1711
1712 /*
1713 * It's possible that the user does not have privileges to CLUSTER the
1714 * leaf partition despite having such privileges on the partitioned
1715 * table. We skip any partitions which the user is not permitted to
1716 * CLUSTER.
1717 */
1719 continue;
1720
1721 /* Use a permanent memory context for the result list */
1722 old_context = MemoryContextSwitchTo(cluster_context);
1723
1724 rtc = (RelToCluster *) palloc(sizeof(RelToCluster));
1725 rtc->tableOid = relid;
1726 rtc->indexOid = indexrelid;
1727 rtcs = lappend(rtcs, rtc);
1728
1729 MemoryContextSwitchTo(old_context);
1730 }
1731
1732 return rtcs;
1733}
Oid IndexGetRelation(Oid indexId, bool missing_ok)
Definition: index.c:3554
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:2003
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
Definition: pg_inherits.c:255

References cluster_is_permitted_for_relation(), find_all_inheritors(), get_rel_relkind(), GetUserId(), IndexGetRelation(), RelToCluster::indexOid, lappend(), lfirst_oid, MemoryContextSwitchTo(), NIL, NoLock, palloc(), and RelToCluster::tableOid.

Referenced by cluster().

◆ make_new_heap()

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

Definition at line 705 of file cluster.c.

707{
708 TupleDesc OldHeapDesc;
709 char NewHeapName[NAMEDATALEN];
710 Oid OIDNewHeap;
711 Oid toastid;
712 Relation OldHeap;
713 HeapTuple tuple;
714 Datum reloptions;
715 bool isNull;
716 Oid namespaceid;
717
718 OldHeap = table_open(OIDOldHeap, lockmode);
719 OldHeapDesc = RelationGetDescr(OldHeap);
720
721 /*
722 * Note that the NewHeap will not receive any of the defaults or
723 * constraints associated with the OldHeap; we don't need 'em, and there's
724 * no reason to spend cycles inserting them into the catalogs only to
725 * delete them.
726 */
727
728 /*
729 * But we do want to use reloptions of the old heap for new heap.
730 */
731 tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(OIDOldHeap));
732 if (!HeapTupleIsValid(tuple))
733 elog(ERROR, "cache lookup failed for relation %u", OIDOldHeap);
734 reloptions = SysCacheGetAttr(RELOID, tuple, Anum_pg_class_reloptions,
735 &isNull);
736 if (isNull)
737 reloptions = (Datum) 0;
738
739 if (relpersistence == RELPERSISTENCE_TEMP)
740 namespaceid = LookupCreationNamespace("pg_temp");
741 else
742 namespaceid = RelationGetNamespace(OldHeap);
743
744 /*
745 * Create the new heap, using a temporary name in the same namespace as
746 * the existing table. NOTE: there is some risk of collision with user
747 * relnames. Working around this seems more trouble than it's worth; in
748 * particular, we can't create the new heap in a different namespace from
749 * the old, or we will have problems with the TEMP status of temp tables.
750 *
751 * Note: the new heap is not a shared relation, even if we are rebuilding
752 * a shared rel. However, we do make the new heap mapped if the source is
753 * mapped. This simplifies swap_relation_files, and is absolutely
754 * necessary for rebuilding pg_class, for reasons explained there.
755 */
756 snprintf(NewHeapName, sizeof(NewHeapName), "pg_temp_%u", OIDOldHeap);
757
758 OIDNewHeap = heap_create_with_catalog(NewHeapName,
759 namespaceid,
760 NewTableSpace,
764 OldHeap->rd_rel->relowner,
765 NewAccessMethod,
766 OldHeapDesc,
767 NIL,
768 RELKIND_RELATION,
769 relpersistence,
770 false,
771 RelationIsMapped(OldHeap),
773 reloptions,
774 false,
775 true,
776 true,
777 OIDOldHeap,
778 NULL);
779 Assert(OIDNewHeap != InvalidOid);
780
781 ReleaseSysCache(tuple);
782
783 /*
784 * Advance command counter so that the newly-created relation's catalog
785 * tuples will be visible to table_open.
786 */
788
789 /*
790 * If necessary, create a TOAST table for the new relation.
791 *
792 * If the relation doesn't have a TOAST table already, we can't need one
793 * for the new relation. The other way around is possible though: if some
794 * wide columns have been dropped, NewHeapCreateToastTable can decide that
795 * no TOAST table is needed for the new table.
796 *
797 * Note that NewHeapCreateToastTable ends with CommandCounterIncrement, so
798 * that the TOAST table will be visible for insertion.
799 */
800 toastid = OldHeap->rd_rel->reltoastrelid;
801 if (OidIsValid(toastid))
802 {
803 /* keep the existing toast table's reloptions, if any */
804 tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(toastid));
805 if (!HeapTupleIsValid(tuple))
806 elog(ERROR, "cache lookup failed for relation %u", toastid);
807 reloptions = SysCacheGetAttr(RELOID, tuple, Anum_pg_class_reloptions,
808 &isNull);
809 if (isNull)
810 reloptions = (Datum) 0;
811
812 NewHeapCreateToastTable(OIDNewHeap, reloptions, lockmode, toastid);
813
814 ReleaseSysCache(tuple);
815 }
816
817 table_close(OldHeap, NoLock);
818
819 return OIDNewHeap;
820}
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:1098
Oid LookupCreationNamespace(const char *nspname)
Definition: namespace.c:3428
uintptr_t Datum
Definition: postgres.h:69
@ ONCOMMIT_NOOP
Definition: primnodes.h:57
#define RelationIsMapped(relation)
Definition: rel.h:554
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:221
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:600
void NewHeapCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode, Oid OIDOldToast)
Definition: toasting.c:64

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(), SearchSysCache1(), snprintf, SysCacheGetAttr(), table_close(), and table_open().

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

◆ mark_index_clustered()

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

Definition at line 554 of file cluster.c.

555{
556 HeapTuple indexTuple;
557 Form_pg_index indexForm;
558 Relation pg_index;
560
561 /* Disallow applying to a partitioned table */
562 if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
564 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
565 errmsg("cannot mark index clustered in partitioned table")));
566
567 /*
568 * If the index is already marked clustered, no need to do anything.
569 */
570 if (OidIsValid(indexOid))
571 {
572 if (get_index_isclustered(indexOid))
573 return;
574 }
575
576 /*
577 * Check each index of the relation and set/clear the bit as needed.
578 */
579 pg_index = table_open(IndexRelationId, RowExclusiveLock);
580
581 foreach(index, RelationGetIndexList(rel))
582 {
583 Oid thisIndexOid = lfirst_oid(index);
584
585 indexTuple = SearchSysCacheCopy1(INDEXRELID,
586 ObjectIdGetDatum(thisIndexOid));
587 if (!HeapTupleIsValid(indexTuple))
588 elog(ERROR, "cache lookup failed for index %u", thisIndexOid);
589 indexForm = (Form_pg_index) GETSTRUCT(indexTuple);
590
591 /*
592 * Unset the bit if set. We know it's wrong because we checked this
593 * earlier.
594 */
595 if (indexForm->indisclustered)
596 {
597 indexForm->indisclustered = false;
598 CatalogTupleUpdate(pg_index, &indexTuple->t_self, indexTuple);
599 }
600 else if (thisIndexOid == indexOid)
601 {
602 /* this was checked earlier, but let's be real sure */
603 if (!indexForm->indisvalid)
604 elog(ERROR, "cannot cluster on invalid index %u", indexOid);
605 indexForm->indisclustered = true;
606 CatalogTupleUpdate(pg_index, &indexTuple->t_self, indexTuple);
607 }
608
609 InvokeObjectPostAlterHookArg(IndexRelationId, thisIndexOid, 0,
610 InvalidOid, is_internal);
611
612 heap_freetuple(indexTuple);
613 }
614
615 table_close(pg_index, RowExclusiveLock);
616}
#define InvokeObjectPostAlterHookArg(classId, objectId, subId, auxiliaryId, is_internal)
Definition: objectaccess.h:200

References CatalogTupleUpdate(), elog, ereport, errcode(), errmsg(), ERROR, get_index_isclustered(), GETSTRUCT, heap_freetuple(), HeapTupleIsValid, 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().

◆ rebuild_relation()

static void rebuild_relation ( Relation  OldHeap,
Relation  index,
bool  verbose 
)
static

Definition at line 629 of file cluster.c.

630{
631 Oid tableOid = RelationGetRelid(OldHeap);
632 Oid accessMethod = OldHeap->rd_rel->relam;
633 Oid tableSpace = OldHeap->rd_rel->reltablespace;
634 Oid OIDNewHeap;
635 Relation NewHeap;
636 char relpersistence;
637 bool is_system_catalog;
638 bool swap_toast_by_content;
639 TransactionId frozenXid;
640 MultiXactId cutoffMulti;
641
644
645 if (index)
646 /* Mark the correct index as clustered */
648
649 /* Remember info about rel before closing OldHeap */
650 relpersistence = OldHeap->rd_rel->relpersistence;
651 is_system_catalog = IsSystemRelation(OldHeap);
652
653 /*
654 * Create the transient table that will receive the re-ordered data.
655 *
656 * OldHeap is already locked, so no need to lock it again. make_new_heap
657 * obtains AccessExclusiveLock on the new heap and its toast table.
658 */
659 OIDNewHeap = make_new_heap(tableOid, tableSpace,
660 accessMethod,
661 relpersistence,
662 NoLock);
664 NewHeap = table_open(OIDNewHeap, NoLock);
665
666 /* Copy the heap data into the new table in the desired order */
667 copy_table_data(NewHeap, OldHeap, index, verbose,
668 &swap_toast_by_content, &frozenXid, &cutoffMulti);
669
670
671 /* Close relcache entries, but keep lock until transaction commit */
672 table_close(OldHeap, NoLock);
673 if (index)
675
676 /*
677 * Close the new relation so it can be dropped as soon as the storage is
678 * swapped. The relation is not visible to others, so no need to unlock it
679 * explicitly.
680 */
681 table_close(NewHeap, NoLock);
682
683 /*
684 * Swap the physical files of the target and transient tables, then
685 * rebuild the target's indexes and throw away the transient table.
686 */
687 finish_heap_swap(tableOid, OIDNewHeap, is_system_catalog,
688 swap_toast_by_content, false, true,
689 frozenXid, cutoffMulti,
690 relpersistence);
691}
bool IsSystemRelation(Relation relation)
Definition: catalog.c:73
static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldIndex, bool verbose, bool *pSwapToastByContent, TransactionId *pFreezeXid, MultiXactId *pCutoffMulti)
Definition: cluster.c:831
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:1440
Oid make_new_heap(Oid OIDOldHeap, Oid NewTableSpace, Oid NewAccessMethod, char relpersistence, LOCKMODE lockmode)
Definition: cluster.c:705
void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal)
Definition: cluster.c:554
bool CheckRelationOidLockedByMe(Oid relid, LOCKMODE lockmode, bool orstronger)
Definition: lmgr.c:346

References AccessExclusiveLock, Assert, CheckRelationLockedByMe(), CheckRelationOidLockedByMe(), copy_table_data(), finish_heap_swap(), index_close(), IsSystemRelation(), make_new_heap(), mark_index_clustered(), NoLock, RelationData::rd_rel, RelationGetRelid, table_close(), table_open(), and verbose.

Referenced by cluster_rel().

◆ swap_relation_files()

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

Definition at line 1063 of file cluster.c.

1069{
1070 Relation relRelation;
1071 HeapTuple reltup1,
1072 reltup2;
1073 Form_pg_class relform1,
1074 relform2;
1075 RelFileNumber relfilenumber1,
1076 relfilenumber2;
1077 RelFileNumber swaptemp;
1078 char swptmpchr;
1079 Oid relam1,
1080 relam2;
1081
1082 /* We need writable copies of both pg_class tuples. */
1083 relRelation = table_open(RelationRelationId, RowExclusiveLock);
1084
1085 reltup1 = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(r1));
1086 if (!HeapTupleIsValid(reltup1))
1087 elog(ERROR, "cache lookup failed for relation %u", r1);
1088 relform1 = (Form_pg_class) GETSTRUCT(reltup1);
1089
1090 reltup2 = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(r2));
1091 if (!HeapTupleIsValid(reltup2))
1092 elog(ERROR, "cache lookup failed for relation %u", r2);
1093 relform2 = (Form_pg_class) GETSTRUCT(reltup2);
1094
1095 relfilenumber1 = relform1->relfilenode;
1096 relfilenumber2 = relform2->relfilenode;
1097 relam1 = relform1->relam;
1098 relam2 = relform2->relam;
1099
1100 if (RelFileNumberIsValid(relfilenumber1) &&
1101 RelFileNumberIsValid(relfilenumber2))
1102 {
1103 /*
1104 * Normal non-mapped relations: swap relfilenumbers, reltablespaces,
1105 * relpersistence
1106 */
1107 Assert(!target_is_pg_class);
1108
1109 swaptemp = relform1->relfilenode;
1110 relform1->relfilenode = relform2->relfilenode;
1111 relform2->relfilenode = swaptemp;
1112
1113 swaptemp = relform1->reltablespace;
1114 relform1->reltablespace = relform2->reltablespace;
1115 relform2->reltablespace = swaptemp;
1116
1117 swaptemp = relform1->relam;
1118 relform1->relam = relform2->relam;
1119 relform2->relam = swaptemp;
1120
1121 swptmpchr = relform1->relpersistence;
1122 relform1->relpersistence = relform2->relpersistence;
1123 relform2->relpersistence = swptmpchr;
1124
1125 /* Also swap toast links, if we're swapping by links */
1126 if (!swap_toast_by_content)
1127 {
1128 swaptemp = relform1->reltoastrelid;
1129 relform1->reltoastrelid = relform2->reltoastrelid;
1130 relform2->reltoastrelid = swaptemp;
1131 }
1132 }
1133 else
1134 {
1135 /*
1136 * Mapped-relation case. Here we have to swap the relation mappings
1137 * instead of modifying the pg_class columns. Both must be mapped.
1138 */
1139 if (RelFileNumberIsValid(relfilenumber1) ||
1140 RelFileNumberIsValid(relfilenumber2))
1141 elog(ERROR, "cannot swap mapped relation \"%s\" with non-mapped relation",
1142 NameStr(relform1->relname));
1143
1144 /*
1145 * We can't change the tablespace nor persistence of a mapped rel, and
1146 * we can't handle toast link swapping for one either, because we must
1147 * not apply any critical changes to its pg_class row. These cases
1148 * should be prevented by upstream permissions tests, so these checks
1149 * are non-user-facing emergency backstop.
1150 */
1151 if (relform1->reltablespace != relform2->reltablespace)
1152 elog(ERROR, "cannot change tablespace of mapped relation \"%s\"",
1153 NameStr(relform1->relname));
1154 if (relform1->relpersistence != relform2->relpersistence)
1155 elog(ERROR, "cannot change persistence of mapped relation \"%s\"",
1156 NameStr(relform1->relname));
1157 if (relform1->relam != relform2->relam)
1158 elog(ERROR, "cannot change access method of mapped relation \"%s\"",
1159 NameStr(relform1->relname));
1160 if (!swap_toast_by_content &&
1161 (relform1->reltoastrelid || relform2->reltoastrelid))
1162 elog(ERROR, "cannot swap toast by links for mapped relation \"%s\"",
1163 NameStr(relform1->relname));
1164
1165 /*
1166 * Fetch the mappings --- shouldn't fail, but be paranoid
1167 */
1168 relfilenumber1 = RelationMapOidToFilenumber(r1, relform1->relisshared);
1169 if (!RelFileNumberIsValid(relfilenumber1))
1170 elog(ERROR, "could not find relation mapping for relation \"%s\", OID %u",
1171 NameStr(relform1->relname), r1);
1172 relfilenumber2 = RelationMapOidToFilenumber(r2, relform2->relisshared);
1173 if (!RelFileNumberIsValid(relfilenumber2))
1174 elog(ERROR, "could not find relation mapping for relation \"%s\", OID %u",
1175 NameStr(relform2->relname), r2);
1176
1177 /*
1178 * Send replacement mappings to relmapper. Note these won't actually
1179 * take effect until CommandCounterIncrement.
1180 */
1181 RelationMapUpdateMap(r1, relfilenumber2, relform1->relisshared, false);
1182 RelationMapUpdateMap(r2, relfilenumber1, relform2->relisshared, false);
1183
1184 /* Pass OIDs of mapped r2 tables back to caller */
1185 *mapped_tables++ = r2;
1186 }
1187
1188 /*
1189 * Recognize that rel1's relfilenumber (swapped from rel2) is new in this
1190 * subtransaction. The rel2 storage (swapped from rel1) may or may not be
1191 * new.
1192 */
1193 {
1194 Relation rel1,
1195 rel2;
1196
1197 rel1 = relation_open(r1, NoLock);
1198 rel2 = relation_open(r2, NoLock);
1199 rel2->rd_createSubid = rel1->rd_createSubid;
1203 relation_close(rel1, NoLock);
1204 relation_close(rel2, NoLock);
1205 }
1206
1207 /*
1208 * In the case of a shared catalog, these next few steps will only affect
1209 * our own database's pg_class row; but that's okay, because they are all
1210 * noncritical updates. That's also an important fact for the case of a
1211 * mapped catalog, because it's possible that we'll commit the map change
1212 * and then fail to commit the pg_class update.
1213 */
1214
1215 /* set rel1's frozen Xid and minimum MultiXid */
1216 if (relform1->relkind != RELKIND_INDEX)
1217 {
1218 Assert(!TransactionIdIsValid(frozenXid) ||
1219 TransactionIdIsNormal(frozenXid));
1220 relform1->relfrozenxid = frozenXid;
1221 relform1->relminmxid = cutoffMulti;
1222 }
1223
1224 /* swap size statistics too, since new rel has freshly-updated stats */
1225 {
1226 int32 swap_pages;
1227 float4 swap_tuples;
1228 int32 swap_allvisible;
1229
1230 swap_pages = relform1->relpages;
1231 relform1->relpages = relform2->relpages;
1232 relform2->relpages = swap_pages;
1233
1234 swap_tuples = relform1->reltuples;
1235 relform1->reltuples = relform2->reltuples;
1236 relform2->reltuples = swap_tuples;
1237
1238 swap_allvisible = relform1->relallvisible;
1239 relform1->relallvisible = relform2->relallvisible;
1240 relform2->relallvisible = swap_allvisible;
1241 }
1242
1243 /*
1244 * Update the tuples in pg_class --- unless the target relation of the
1245 * swap is pg_class itself. In that case, there is zero point in making
1246 * changes because we'd be updating the old data that we're about to throw
1247 * away. Because the real work being done here for a mapped relation is
1248 * just to change the relation map settings, it's all right to not update
1249 * the pg_class rows in this case. The most important changes will instead
1250 * performed later, in finish_heap_swap() itself.
1251 */
1252 if (!target_is_pg_class)
1253 {
1254 CatalogIndexState indstate;
1255
1256 indstate = CatalogOpenIndexes(relRelation);
1257 CatalogTupleUpdateWithInfo(relRelation, &reltup1->t_self, reltup1,
1258 indstate);
1259 CatalogTupleUpdateWithInfo(relRelation, &reltup2->t_self, reltup2,
1260 indstate);
1261 CatalogCloseIndexes(indstate);
1262 }
1263 else
1264 {
1265 /* no update ... but we do still need relcache inval */
1268 }
1269
1270 /*
1271 * Now that pg_class has been updated with its relevant information for
1272 * the swap, update the dependency of the relations to point to their new
1273 * table AM, if it has changed.
1274 */
1275 if (relam1 != relam2)
1276 {
1277 if (changeDependencyFor(RelationRelationId,
1278 r1,
1279 AccessMethodRelationId,
1280 relam1,
1281 relam2) != 1)
1282 elog(ERROR, "could not change access method dependency for relation \"%s.%s\"",
1284 get_rel_name(r1));
1285 if (changeDependencyFor(RelationRelationId,
1286 r2,
1287 AccessMethodRelationId,
1288 relam2,
1289 relam1) != 1)
1290 elog(ERROR, "could not change access method dependency for relation \"%s.%s\"",
1292 get_rel_name(r2));
1293 }
1294
1295 /*
1296 * Post alter hook for modified relations. The change to r2 is always
1297 * internal, but r1 depends on the invocation context.
1298 */
1299 InvokeObjectPostAlterHookArg(RelationRelationId, r1, 0,
1300 InvalidOid, is_internal);
1301 InvokeObjectPostAlterHookArg(RelationRelationId, r2, 0,
1302 InvalidOid, true);
1303
1304 /*
1305 * If we have toast tables associated with the relations being swapped,
1306 * deal with them too.
1307 */
1308 if (relform1->reltoastrelid || relform2->reltoastrelid)
1309 {
1310 if (swap_toast_by_content)
1311 {
1312 if (relform1->reltoastrelid && relform2->reltoastrelid)
1313 {
1314 /* Recursively swap the contents of the toast tables */
1315 swap_relation_files(relform1->reltoastrelid,
1316 relform2->reltoastrelid,
1317 target_is_pg_class,
1318 swap_toast_by_content,
1319 is_internal,
1320 frozenXid,
1321 cutoffMulti,
1322 mapped_tables);
1323 }
1324 else
1325 {
1326 /* caller messed up */
1327 elog(ERROR, "cannot swap toast files by content when there's only one");
1328 }
1329 }
1330 else
1331 {
1332 /*
1333 * We swapped the ownership links, so we need to change dependency
1334 * data to match.
1335 *
1336 * NOTE: it is possible that only one table has a toast table.
1337 *
1338 * NOTE: at present, a TOAST table's only dependency is the one on
1339 * its owning table. If more are ever created, we'd need to use
1340 * something more selective than deleteDependencyRecordsFor() to
1341 * get rid of just the link we want.
1342 */
1343 ObjectAddress baseobject,
1344 toastobject;
1345 long count;
1346
1347 /*
1348 * We disallow this case for system catalogs, to avoid the
1349 * possibility that the catalog we're rebuilding is one of the
1350 * ones the dependency changes would change. It's too late to be
1351 * making any data changes to the target catalog.
1352 */
1353 if (IsSystemClass(r1, relform1))
1354 elog(ERROR, "cannot swap toast files by links for system catalogs");
1355
1356 /* Delete old dependencies */
1357 if (relform1->reltoastrelid)
1358 {
1359 count = deleteDependencyRecordsFor(RelationRelationId,
1360 relform1->reltoastrelid,
1361 false);
1362 if (count != 1)
1363 elog(ERROR, "expected one dependency record for TOAST table, found %ld",
1364 count);
1365 }
1366 if (relform2->reltoastrelid)
1367 {
1368 count = deleteDependencyRecordsFor(RelationRelationId,
1369 relform2->reltoastrelid,
1370 false);
1371 if (count != 1)
1372 elog(ERROR, "expected one dependency record for TOAST table, found %ld",
1373 count);
1374 }
1375
1376 /* Register new dependencies */
1377 baseobject.classId = RelationRelationId;
1378 baseobject.objectSubId = 0;
1379 toastobject.classId = RelationRelationId;
1380 toastobject.objectSubId = 0;
1381
1382 if (relform1->reltoastrelid)
1383 {
1384 baseobject.objectId = r1;
1385 toastobject.objectId = relform1->reltoastrelid;
1386 recordDependencyOn(&toastobject, &baseobject,
1388 }
1389
1390 if (relform2->reltoastrelid)
1391 {
1392 baseobject.objectId = r2;
1393 toastobject.objectId = relform2->reltoastrelid;
1394 recordDependencyOn(&toastobject, &baseobject,
1396 }
1397 }
1398 }
1399
1400 /*
1401 * If we're swapping two toast tables by content, do the same for their
1402 * valid index. The swap can actually be safely done only if the relations
1403 * have indexes.
1404 */
1405 if (swap_toast_by_content &&
1406 relform1->relkind == RELKIND_TOASTVALUE &&
1407 relform2->relkind == RELKIND_TOASTVALUE)
1408 {
1409 Oid toastIndex1,
1410 toastIndex2;
1411
1412 /* Get valid index for each relation */
1413 toastIndex1 = toast_get_valid_index(r1,
1415 toastIndex2 = toast_get_valid_index(r2,
1417
1418 swap_relation_files(toastIndex1,
1419 toastIndex2,
1420 target_is_pg_class,
1421 swap_toast_by_content,
1422 is_internal,
1425 mapped_tables);
1426 }
1427
1428 /* Clean up. */
1429 heap_freetuple(reltup1);
1430 heap_freetuple(reltup2);
1431
1432 table_close(relRelation, RowExclusiveLock);
1433}
#define NameStr(name)
Definition: c.h:703
int32_t int32
Definition: c.h:484
float float4
Definition: c.h:586
bool IsSystemClass(Oid relid, Form_pg_class reltuple)
Definition: catalog.c:85
@ DEPENDENCY_INTERNAL
Definition: dependency.h:35
void CatalogCloseIndexes(CatalogIndexState indstate)
Definition: indexing.c:61
CatalogIndexState CatalogOpenIndexes(Relation heapRel)
Definition: indexing.c:43
void CatalogTupleUpdateWithInfo(Relation heapRel, ItemPointer otid, HeapTuple tup, CatalogIndexState indstate)
Definition: indexing.c:337
Oid get_rel_namespace(Oid relid)
Definition: lsyscache.c:1952
#define InvalidMultiXactId
Definition: multixact.h:24
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:45
long changeDependencyFor(Oid classId, Oid objectId, Oid refClassId, Oid oldRefObjectId, Oid newRefObjectId)
Definition: pg_depend.c:457
long deleteDependencyRecordsFor(Oid classId, Oid objectId, bool skipExtensionDeps)
Definition: pg_depend.c:301
void RelationAssumeNewRelfilelocator(Relation relation)
Definition: relcache.c:3920
RelFileNumber RelationMapOidToFilenumber(Oid relationId, bool shared)
Definition: relmapper.c:165
void RelationMapUpdateMap(Oid relationId, RelFileNumber fileNumber, bool shared, bool immediate)
Definition: relmapper.c:325
Oid RelFileNumber
Definition: relpath.h:25
#define RelFileNumberIsValid(relnumber)
Definition: relpath.h:27
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:47
SubTransactionId rd_firstRelfilelocatorSubid
Definition: rel.h:106
SubTransactionId rd_newRelfilelocatorSubid
Definition: rel.h:104
SubTransactionId rd_createSubid
Definition: rel.h:103
#define InvalidTransactionId
Definition: transam.h:31
#define TransactionIdIsNormal(xid)
Definition: transam.h:42

References AccessExclusiveLock, Assert, CacheInvalidateRelcacheByTuple(), CatalogCloseIndexes(), CatalogOpenIndexes(), CatalogTupleUpdateWithInfo(), changeDependencyFor(), ObjectAddress::classId, deleteDependencyRecordsFor(), DEPENDENCY_INTERNAL, elog, ERROR, get_namespace_name(), get_rel_name(), get_rel_namespace(), GETSTRUCT, heap_freetuple(), HeapTupleIsValid, InvalidMultiXactId, InvalidOid, InvalidTransactionId, InvokeObjectPostAlterHookArg, IsSystemClass(), NameStr, NoLock, ObjectAddress::objectId, ObjectIdGetDatum(), ObjectAddress::objectSubId, RelationData::rd_createSubid, RelationData::rd_firstRelfilelocatorSubid, RelationData::rd_newRelfilelocatorSubid, recordDependencyOn(), relation_close(), relation_open(), RelationAssumeNewRelfilelocator(), RelationMapOidToFilenumber(), RelationMapUpdateMap(), RelFileNumberIsValid, RowExclusiveLock, SearchSysCacheCopy1, swap_relation_files(), HeapTupleData::t_self, table_close(), table_open(), toast_get_valid_index(), TransactionIdIsNormal, and TransactionIdIsValid.

Referenced by finish_heap_swap(), and swap_relation_files().