When creating materialized views, use REFRESH to load data.
[pgsql.git] / src / backend / commands / indexcmds.c
blobc5a56c75f699521a85be73511ac4931c0e13e9b1
1 /*-------------------------------------------------------------------------
3 * indexcmds.c
4 * POSTGRES define and remove index code.
6 * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * IDENTIFICATION
11 * src/backend/commands/indexcmds.c
13 *-------------------------------------------------------------------------
16 #include "postgres.h"
18 #include "access/amapi.h"
19 #include "access/heapam.h"
20 #include "access/htup_details.h"
21 #include "access/reloptions.h"
22 #include "access/sysattr.h"
23 #include "access/tableam.h"
24 #include "access/xact.h"
25 #include "catalog/catalog.h"
26 #include "catalog/index.h"
27 #include "catalog/indexing.h"
28 #include "catalog/namespace.h"
29 #include "catalog/pg_am.h"
30 #include "catalog/pg_authid.h"
31 #include "catalog/pg_constraint.h"
32 #include "catalog/pg_database.h"
33 #include "catalog/pg_inherits.h"
34 #include "catalog/pg_namespace.h"
35 #include "catalog/pg_opclass.h"
36 #include "catalog/pg_opfamily.h"
37 #include "catalog/pg_tablespace.h"
38 #include "catalog/pg_type.h"
39 #include "commands/comment.h"
40 #include "commands/dbcommands.h"
41 #include "commands/defrem.h"
42 #include "commands/event_trigger.h"
43 #include "commands/progress.h"
44 #include "commands/tablecmds.h"
45 #include "commands/tablespace.h"
46 #include "mb/pg_wchar.h"
47 #include "miscadmin.h"
48 #include "nodes/makefuncs.h"
49 #include "nodes/nodeFuncs.h"
50 #include "optimizer/optimizer.h"
51 #include "parser/parse_coerce.h"
52 #include "parser/parse_oper.h"
53 #include "partitioning/partdesc.h"
54 #include "pgstat.h"
55 #include "rewrite/rewriteManip.h"
56 #include "storage/lmgr.h"
57 #include "storage/proc.h"
58 #include "storage/procarray.h"
59 #include "storage/sinvaladt.h"
60 #include "utils/acl.h"
61 #include "utils/builtins.h"
62 #include "utils/fmgroids.h"
63 #include "utils/guc.h"
64 #include "utils/inval.h"
65 #include "utils/lsyscache.h"
66 #include "utils/memutils.h"
67 #include "utils/partcache.h"
68 #include "utils/pg_rusage.h"
69 #include "utils/regproc.h"
70 #include "utils/snapmgr.h"
71 #include "utils/syscache.h"
74 /* non-export function prototypes */
75 static bool CompareOpclassOptions(const Datum *opts1, const Datum *opts2, int natts);
76 static void CheckPredicate(Expr *predicate);
77 static void ComputeIndexAttrs(IndexInfo *indexInfo,
78 Oid *typeOids,
79 Oid *collationOids,
80 Oid *opclassOids,
81 Datum *opclassOptions,
82 int16 *colOptions,
83 const List *attList,
84 const List *exclusionOpNames,
85 Oid relId,
86 const char *accessMethodName,
87 Oid accessMethodId,
88 bool amcanorder,
89 bool isconstraint,
90 Oid ddl_userid,
91 int ddl_sec_context,
92 int *ddl_save_nestlevel);
93 static char *ChooseIndexName(const char *tabname, Oid namespaceId,
94 const List *colnames, const List *exclusionOpNames,
95 bool primary, bool isconstraint);
96 static char *ChooseIndexNameAddition(const List *colnames);
97 static List *ChooseIndexColumnNames(const List *indexElems);
98 static void ReindexIndex(const ReindexStmt *stmt, const ReindexParams *params,
99 bool isTopLevel);
100 static void RangeVarCallbackForReindexIndex(const RangeVar *relation,
101 Oid relId, Oid oldRelId, void *arg);
102 static Oid ReindexTable(const ReindexStmt *stmt, const ReindexParams *params,
103 bool isTopLevel);
104 static void ReindexMultipleTables(const ReindexStmt *stmt,
105 const ReindexParams *params);
106 static void reindex_error_callback(void *arg);
107 static void ReindexPartitions(const ReindexStmt *stmt, Oid relid,
108 const ReindexParams *params, bool isTopLevel);
109 static void ReindexMultipleInternal(const ReindexStmt *stmt, const List *relids,
110 const ReindexParams *params);
111 static bool ReindexRelationConcurrently(const ReindexStmt *stmt,
112 Oid relationOid,
113 const ReindexParams *params);
114 static void update_relispartition(Oid relationId, bool newval);
115 static inline void set_indexsafe_procflags(void);
118 * callback argument type for RangeVarCallbackForReindexIndex()
120 struct ReindexIndexCallbackState
122 ReindexParams params; /* options from statement */
123 Oid locked_table_oid; /* tracks previously locked table */
127 * callback arguments for reindex_error_callback()
129 typedef struct ReindexErrorInfo
131 char *relname;
132 char *relnamespace;
133 char relkind;
134 } ReindexErrorInfo;
137 * CheckIndexCompatible
138 * Determine whether an existing index definition is compatible with a
139 * prospective index definition, such that the existing index storage
140 * could become the storage of the new index, avoiding a rebuild.
142 * 'oldId': the OID of the existing index
143 * 'accessMethodName': name of the AM to use.
144 * 'attributeList': a list of IndexElem specifying columns and expressions
145 * to index on.
146 * 'exclusionOpNames': list of names of exclusion-constraint operators,
147 * or NIL if not an exclusion constraint.
149 * This is tailored to the needs of ALTER TABLE ALTER TYPE, which recreates
150 * any indexes that depended on a changing column from their pg_get_indexdef
151 * or pg_get_constraintdef definitions. We omit some of the sanity checks of
152 * DefineIndex. We assume that the old and new indexes have the same number
153 * of columns and that if one has an expression column or predicate, both do.
154 * Errors arising from the attribute list still apply.
156 * Most column type changes that can skip a table rewrite do not invalidate
157 * indexes. We acknowledge this when all operator classes, collations and
158 * exclusion operators match. Though we could further permit intra-opfamily
159 * changes for btree and hash indexes, that adds subtle complexity with no
160 * concrete benefit for core types. Note, that INCLUDE columns aren't
161 * checked by this function, for them it's enough that table rewrite is
162 * skipped.
164 * When a comparison or exclusion operator has a polymorphic input type, the
165 * actual input types must also match. This defends against the possibility
166 * that operators could vary behavior in response to get_fn_expr_argtype().
167 * At present, this hazard is theoretical: check_exclusion_constraint() and
168 * all core index access methods decline to set fn_expr for such calls.
170 * We do not yet implement a test to verify compatibility of expression
171 * columns or predicates, so assume any such index is incompatible.
173 bool
174 CheckIndexCompatible(Oid oldId,
175 const char *accessMethodName,
176 const List *attributeList,
177 const List *exclusionOpNames)
179 bool isconstraint;
180 Oid *typeIds;
181 Oid *collationIds;
182 Oid *opclassIds;
183 Datum *opclassOptions;
184 Oid accessMethodId;
185 Oid relationId;
186 HeapTuple tuple;
187 Form_pg_index indexForm;
188 Form_pg_am accessMethodForm;
189 IndexAmRoutine *amRoutine;
190 bool amcanorder;
191 bool amsummarizing;
192 int16 *coloptions;
193 IndexInfo *indexInfo;
194 int numberOfAttributes;
195 int old_natts;
196 bool ret = true;
197 oidvector *old_indclass;
198 oidvector *old_indcollation;
199 Relation irel;
200 int i;
201 Datum d;
203 /* Caller should already have the relation locked in some way. */
204 relationId = IndexGetRelation(oldId, false);
207 * We can pretend isconstraint = false unconditionally. It only serves to
208 * decide the text of an error message that should never happen for us.
210 isconstraint = false;
212 numberOfAttributes = list_length(attributeList);
213 Assert(numberOfAttributes > 0);
214 Assert(numberOfAttributes <= INDEX_MAX_KEYS);
216 /* look up the access method */
217 tuple = SearchSysCache1(AMNAME, PointerGetDatum(accessMethodName));
218 if (!HeapTupleIsValid(tuple))
219 ereport(ERROR,
220 (errcode(ERRCODE_UNDEFINED_OBJECT),
221 errmsg("access method \"%s\" does not exist",
222 accessMethodName)));
223 accessMethodForm = (Form_pg_am) GETSTRUCT(tuple);
224 accessMethodId = accessMethodForm->oid;
225 amRoutine = GetIndexAmRoutine(accessMethodForm->amhandler);
226 ReleaseSysCache(tuple);
228 amcanorder = amRoutine->amcanorder;
229 amsummarizing = amRoutine->amsummarizing;
232 * Compute the operator classes, collations, and exclusion operators for
233 * the new index, so we can test whether it's compatible with the existing
234 * one. Note that ComputeIndexAttrs might fail here, but that's OK:
235 * DefineIndex would have failed later. Our attributeList contains only
236 * key attributes, thus we're filling ii_NumIndexAttrs and
237 * ii_NumIndexKeyAttrs with same value.
239 indexInfo = makeIndexInfo(numberOfAttributes, numberOfAttributes,
240 accessMethodId, NIL, NIL, false, false,
241 false, false, amsummarizing);
242 typeIds = palloc_array(Oid, numberOfAttributes);
243 collationIds = palloc_array(Oid, numberOfAttributes);
244 opclassIds = palloc_array(Oid, numberOfAttributes);
245 opclassOptions = palloc_array(Datum, numberOfAttributes);
246 coloptions = palloc_array(int16, numberOfAttributes);
247 ComputeIndexAttrs(indexInfo,
248 typeIds, collationIds, opclassIds, opclassOptions,
249 coloptions, attributeList,
250 exclusionOpNames, relationId,
251 accessMethodName, accessMethodId,
252 amcanorder, isconstraint, InvalidOid, 0, NULL);
255 /* Get the soon-obsolete pg_index tuple. */
256 tuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(oldId));
257 if (!HeapTupleIsValid(tuple))
258 elog(ERROR, "cache lookup failed for index %u", oldId);
259 indexForm = (Form_pg_index) GETSTRUCT(tuple);
262 * We don't assess expressions or predicates; assume incompatibility.
263 * Also, if the index is invalid for any reason, treat it as incompatible.
265 if (!(heap_attisnull(tuple, Anum_pg_index_indpred, NULL) &&
266 heap_attisnull(tuple, Anum_pg_index_indexprs, NULL) &&
267 indexForm->indisvalid))
269 ReleaseSysCache(tuple);
270 return false;
273 /* Any change in operator class or collation breaks compatibility. */
274 old_natts = indexForm->indnkeyatts;
275 Assert(old_natts == numberOfAttributes);
277 d = SysCacheGetAttrNotNull(INDEXRELID, tuple, Anum_pg_index_indcollation);
278 old_indcollation = (oidvector *) DatumGetPointer(d);
280 d = SysCacheGetAttrNotNull(INDEXRELID, tuple, Anum_pg_index_indclass);
281 old_indclass = (oidvector *) DatumGetPointer(d);
283 ret = (memcmp(old_indclass->values, opclassIds, old_natts * sizeof(Oid)) == 0 &&
284 memcmp(old_indcollation->values, collationIds, old_natts * sizeof(Oid)) == 0);
286 ReleaseSysCache(tuple);
288 if (!ret)
289 return false;
291 /* For polymorphic opcintype, column type changes break compatibility. */
292 irel = index_open(oldId, AccessShareLock); /* caller probably has a lock */
293 for (i = 0; i < old_natts; i++)
295 if (IsPolymorphicType(get_opclass_input_type(opclassIds[i])) &&
296 TupleDescAttr(irel->rd_att, i)->atttypid != typeIds[i])
298 ret = false;
299 break;
303 /* Any change in opclass options break compatibility. */
304 if (ret)
306 Datum *oldOpclassOptions = palloc_array(Datum, old_natts);
308 for (i = 0; i < old_natts; i++)
309 oldOpclassOptions[i] = get_attoptions(oldId, i + 1);
311 ret = CompareOpclassOptions(oldOpclassOptions, opclassOptions, old_natts);
313 pfree(oldOpclassOptions);
316 /* Any change in exclusion operator selections breaks compatibility. */
317 if (ret && indexInfo->ii_ExclusionOps != NULL)
319 Oid *old_operators,
320 *old_procs;
321 uint16 *old_strats;
323 RelationGetExclusionInfo(irel, &old_operators, &old_procs, &old_strats);
324 ret = memcmp(old_operators, indexInfo->ii_ExclusionOps,
325 old_natts * sizeof(Oid)) == 0;
327 /* Require an exact input type match for polymorphic operators. */
328 if (ret)
330 for (i = 0; i < old_natts && ret; i++)
332 Oid left,
333 right;
335 op_input_types(indexInfo->ii_ExclusionOps[i], &left, &right);
336 if ((IsPolymorphicType(left) || IsPolymorphicType(right)) &&
337 TupleDescAttr(irel->rd_att, i)->atttypid != typeIds[i])
339 ret = false;
340 break;
346 index_close(irel, NoLock);
347 return ret;
351 * CompareOpclassOptions
353 * Compare per-column opclass options which are represented by arrays of text[]
354 * datums. Both elements of arrays and array themselves can be NULL.
356 static bool
357 CompareOpclassOptions(const Datum *opts1, const Datum *opts2, int natts)
359 int i;
361 if (!opts1 && !opts2)
362 return true;
364 for (i = 0; i < natts; i++)
366 Datum opt1 = opts1 ? opts1[i] : (Datum) 0;
367 Datum opt2 = opts2 ? opts2[i] : (Datum) 0;
369 if (opt1 == (Datum) 0)
371 if (opt2 == (Datum) 0)
372 continue;
373 else
374 return false;
376 else if (opt2 == (Datum) 0)
377 return false;
379 /* Compare non-NULL text[] datums. */
380 if (!DatumGetBool(DirectFunctionCall2(array_eq, opt1, opt2)))
381 return false;
384 return true;
388 * WaitForOlderSnapshots
390 * Wait for transactions that might have an older snapshot than the given xmin
391 * limit, because it might not contain tuples deleted just before it has
392 * been taken. Obtain a list of VXIDs of such transactions, and wait for them
393 * individually. This is used when building an index concurrently.
395 * We can exclude any running transactions that have xmin > the xmin given;
396 * their oldest snapshot must be newer than our xmin limit.
397 * We can also exclude any transactions that have xmin = zero, since they
398 * evidently have no live snapshot at all (and any one they might be in
399 * process of taking is certainly newer than ours). Transactions in other
400 * DBs can be ignored too, since they'll never even be able to see the
401 * index being worked on.
403 * We can also exclude autovacuum processes and processes running manual
404 * lazy VACUUMs, because they won't be fazed by missing index entries
405 * either. (Manual ANALYZEs, however, can't be excluded because they
406 * might be within transactions that are going to do arbitrary operations
407 * later.) Processes running CREATE INDEX CONCURRENTLY or REINDEX CONCURRENTLY
408 * on indexes that are neither expressional nor partial are also safe to
409 * ignore, since we know that those processes won't examine any data
410 * outside the table they're indexing.
412 * Also, GetCurrentVirtualXIDs never reports our own vxid, so we need not
413 * check for that.
415 * If a process goes idle-in-transaction with xmin zero, we do not need to
416 * wait for it anymore, per the above argument. We do not have the
417 * infrastructure right now to stop waiting if that happens, but we can at
418 * least avoid the folly of waiting when it is idle at the time we would
419 * begin to wait. We do this by repeatedly rechecking the output of
420 * GetCurrentVirtualXIDs. If, during any iteration, a particular vxid
421 * doesn't show up in the output, we know we can forget about it.
423 void
424 WaitForOlderSnapshots(TransactionId limitXmin, bool progress)
426 int n_old_snapshots;
427 int i;
428 VirtualTransactionId *old_snapshots;
430 old_snapshots = GetCurrentVirtualXIDs(limitXmin, true, false,
431 PROC_IS_AUTOVACUUM | PROC_IN_VACUUM
432 | PROC_IN_SAFE_IC,
433 &n_old_snapshots);
434 if (progress)
435 pgstat_progress_update_param(PROGRESS_WAITFOR_TOTAL, n_old_snapshots);
437 for (i = 0; i < n_old_snapshots; i++)
439 if (!VirtualTransactionIdIsValid(old_snapshots[i]))
440 continue; /* found uninteresting in previous cycle */
442 if (i > 0)
444 /* see if anything's changed ... */
445 VirtualTransactionId *newer_snapshots;
446 int n_newer_snapshots;
447 int j;
448 int k;
450 newer_snapshots = GetCurrentVirtualXIDs(limitXmin,
451 true, false,
452 PROC_IS_AUTOVACUUM | PROC_IN_VACUUM
453 | PROC_IN_SAFE_IC,
454 &n_newer_snapshots);
455 for (j = i; j < n_old_snapshots; j++)
457 if (!VirtualTransactionIdIsValid(old_snapshots[j]))
458 continue; /* found uninteresting in previous cycle */
459 for (k = 0; k < n_newer_snapshots; k++)
461 if (VirtualTransactionIdEquals(old_snapshots[j],
462 newer_snapshots[k]))
463 break;
465 if (k >= n_newer_snapshots) /* not there anymore */
466 SetInvalidVirtualTransactionId(old_snapshots[j]);
468 pfree(newer_snapshots);
471 if (VirtualTransactionIdIsValid(old_snapshots[i]))
473 /* If requested, publish who we're going to wait for. */
474 if (progress)
476 PGPROC *holder = ProcNumberGetProc(old_snapshots[i].procNumber);
478 if (holder)
479 pgstat_progress_update_param(PROGRESS_WAITFOR_CURRENT_PID,
480 holder->pid);
482 VirtualXactLock(old_snapshots[i], true);
485 if (progress)
486 pgstat_progress_update_param(PROGRESS_WAITFOR_DONE, i + 1);
492 * DefineIndex
493 * Creates a new index.
495 * This function manages the current userid according to the needs of pg_dump.
496 * Recreating old-database catalog entries in new-database is fine, regardless
497 * of which users would have permission to recreate those entries now. That's
498 * just preservation of state. Running opaque expressions, like calling a
499 * function named in a catalog entry or evaluating a pg_node_tree in a catalog
500 * entry, as anyone other than the object owner, is not fine. To adhere to
501 * those principles and to remain fail-safe, use the table owner userid for
502 * most ACL checks. Use the original userid for ACL checks reached without
503 * traversing opaque expressions. (pg_dump can predict such ACL checks from
504 * catalogs.) Overall, this is a mess. Future DDL development should
505 * consider offering one DDL command for catalog setup and a separate DDL
506 * command for steps that run opaque expressions.
508 * 'tableId': the OID of the table relation on which the index is to be
509 * created
510 * 'stmt': IndexStmt describing the properties of the new index.
511 * 'indexRelationId': normally InvalidOid, but during bootstrap can be
512 * nonzero to specify a preselected OID for the index.
513 * 'parentIndexId': the OID of the parent index; InvalidOid if not the child
514 * of a partitioned index.
515 * 'parentConstraintId': the OID of the parent constraint; InvalidOid if not
516 * the child of a constraint (only used when recursing)
517 * 'total_parts': total number of direct and indirect partitions of relation;
518 * pass -1 if not known or rel is not partitioned.
519 * 'is_alter_table': this is due to an ALTER rather than a CREATE operation.
520 * 'check_rights': check for CREATE rights in namespace and tablespace. (This
521 * should be true except when ALTER is deleting/recreating an index.)
522 * 'check_not_in_use': check for table not already in use in current session.
523 * This should be true unless caller is holding the table open, in which
524 * case the caller had better have checked it earlier.
525 * 'skip_build': make the catalog entries but don't create the index files
526 * 'quiet': suppress the NOTICE chatter ordinarily provided for constraints.
528 * Returns the object address of the created index.
530 ObjectAddress
531 DefineIndex(Oid tableId,
532 IndexStmt *stmt,
533 Oid indexRelationId,
534 Oid parentIndexId,
535 Oid parentConstraintId,
536 int total_parts,
537 bool is_alter_table,
538 bool check_rights,
539 bool check_not_in_use,
540 bool skip_build,
541 bool quiet)
543 bool concurrent;
544 char *indexRelationName;
545 char *accessMethodName;
546 Oid *typeIds;
547 Oid *collationIds;
548 Oid *opclassIds;
549 Datum *opclassOptions;
550 Oid accessMethodId;
551 Oid namespaceId;
552 Oid tablespaceId;
553 Oid createdConstraintId = InvalidOid;
554 List *indexColNames;
555 List *allIndexParams;
556 Relation rel;
557 HeapTuple tuple;
558 Form_pg_am accessMethodForm;
559 IndexAmRoutine *amRoutine;
560 bool amcanorder;
561 bool amissummarizing;
562 amoptions_function amoptions;
563 bool partitioned;
564 bool safe_index;
565 Datum reloptions;
566 int16 *coloptions;
567 IndexInfo *indexInfo;
568 bits16 flags;
569 bits16 constr_flags;
570 int numberOfAttributes;
571 int numberOfKeyAttributes;
572 TransactionId limitXmin;
573 ObjectAddress address;
574 LockRelId heaprelid;
575 LOCKTAG heaplocktag;
576 LOCKMODE lockmode;
577 Snapshot snapshot;
578 Oid root_save_userid;
579 int root_save_sec_context;
580 int root_save_nestlevel;
582 root_save_nestlevel = NewGUCNestLevel();
584 RestrictSearchPath();
587 * Some callers need us to run with an empty default_tablespace; this is a
588 * necessary hack to be able to reproduce catalog state accurately when
589 * recreating indexes after table-rewriting ALTER TABLE.
591 if (stmt->reset_default_tblspc)
592 (void) set_config_option("default_tablespace", "",
593 PGC_USERSET, PGC_S_SESSION,
594 GUC_ACTION_SAVE, true, 0, false);
597 * Force non-concurrent build on temporary relations, even if CONCURRENTLY
598 * was requested. Other backends can't access a temporary relation, so
599 * there's no harm in grabbing a stronger lock, and a non-concurrent DROP
600 * is more efficient. Do this before any use of the concurrent option is
601 * done.
603 if (stmt->concurrent && get_rel_persistence(tableId) != RELPERSISTENCE_TEMP)
604 concurrent = true;
605 else
606 concurrent = false;
609 * Start progress report. If we're building a partition, this was already
610 * done.
612 if (!OidIsValid(parentIndexId))
614 pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, tableId);
615 pgstat_progress_update_param(PROGRESS_CREATEIDX_COMMAND,
616 concurrent ?
617 PROGRESS_CREATEIDX_COMMAND_CREATE_CONCURRENTLY :
618 PROGRESS_CREATEIDX_COMMAND_CREATE);
622 * No index OID to report yet
624 pgstat_progress_update_param(PROGRESS_CREATEIDX_INDEX_OID,
625 InvalidOid);
628 * count key attributes in index
630 numberOfKeyAttributes = list_length(stmt->indexParams);
633 * Calculate the new list of index columns including both key columns and
634 * INCLUDE columns. Later we can determine which of these are key
635 * columns, and which are just part of the INCLUDE list by checking the
636 * list position. A list item in a position less than ii_NumIndexKeyAttrs
637 * is part of the key columns, and anything equal to and over is part of
638 * the INCLUDE columns.
640 allIndexParams = list_concat_copy(stmt->indexParams,
641 stmt->indexIncludingParams);
642 numberOfAttributes = list_length(allIndexParams);
644 if (numberOfKeyAttributes <= 0)
645 ereport(ERROR,
646 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
647 errmsg("must specify at least one column")));
648 if (numberOfAttributes > INDEX_MAX_KEYS)
649 ereport(ERROR,
650 (errcode(ERRCODE_TOO_MANY_COLUMNS),
651 errmsg("cannot use more than %d columns in an index",
652 INDEX_MAX_KEYS)));
655 * Only SELECT ... FOR UPDATE/SHARE are allowed while doing a standard
656 * index build; but for concurrent builds we allow INSERT/UPDATE/DELETE
657 * (but not VACUUM).
659 * NB: Caller is responsible for making sure that tableId refers to the
660 * relation on which the index should be built; except in bootstrap mode,
661 * this will typically require the caller to have already locked the
662 * relation. To avoid lock upgrade hazards, that lock should be at least
663 * as strong as the one we take here.
665 * NB: If the lock strength here ever changes, code that is run by
666 * parallel workers under the control of certain particular ambuild
667 * functions will need to be updated, too.
669 lockmode = concurrent ? ShareUpdateExclusiveLock : ShareLock;
670 rel = table_open(tableId, lockmode);
673 * Switch to the table owner's userid, so that any index functions are run
674 * as that user. Also lock down security-restricted operations. We
675 * already arranged to make GUC variable changes local to this command.
677 GetUserIdAndSecContext(&root_save_userid, &root_save_sec_context);
678 SetUserIdAndSecContext(rel->rd_rel->relowner,
679 root_save_sec_context | SECURITY_RESTRICTED_OPERATION);
681 namespaceId = RelationGetNamespace(rel);
683 /* Ensure that it makes sense to index this kind of relation */
684 switch (rel->rd_rel->relkind)
686 case RELKIND_RELATION:
687 case RELKIND_MATVIEW:
688 case RELKIND_PARTITIONED_TABLE:
689 /* OK */
690 break;
691 default:
692 ereport(ERROR,
693 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
694 errmsg("cannot create index on relation \"%s\"",
695 RelationGetRelationName(rel)),
696 errdetail_relkind_not_supported(rel->rd_rel->relkind)));
697 break;
701 * Establish behavior for partitioned tables, and verify sanity of
702 * parameters.
704 * We do not build an actual index in this case; we only create a few
705 * catalog entries. The actual indexes are built by recursing for each
706 * partition.
708 partitioned = rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE;
709 if (partitioned)
712 * Note: we check 'stmt->concurrent' rather than 'concurrent', so that
713 * the error is thrown also for temporary tables. Seems better to be
714 * consistent, even though we could do it on temporary table because
715 * we're not actually doing it concurrently.
717 if (stmt->concurrent)
718 ereport(ERROR,
719 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
720 errmsg("cannot create index on partitioned table \"%s\" concurrently",
721 RelationGetRelationName(rel))));
725 * Don't try to CREATE INDEX on temp tables of other backends.
727 if (RELATION_IS_OTHER_TEMP(rel))
728 ereport(ERROR,
729 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
730 errmsg("cannot create indexes on temporary tables of other sessions")));
733 * Unless our caller vouches for having checked this already, insist that
734 * the table not be in use by our own session, either. Otherwise we might
735 * fail to make entries in the new index (for instance, if an INSERT or
736 * UPDATE is in progress and has already made its list of target indexes).
738 if (check_not_in_use)
739 CheckTableNotInUse(rel, "CREATE INDEX");
742 * Verify we (still) have CREATE rights in the rel's namespace.
743 * (Presumably we did when the rel was created, but maybe not anymore.)
744 * Skip check if caller doesn't want it. Also skip check if
745 * bootstrapping, since permissions machinery may not be working yet.
747 if (check_rights && !IsBootstrapProcessingMode())
749 AclResult aclresult;
751 aclresult = object_aclcheck(NamespaceRelationId, namespaceId, root_save_userid,
752 ACL_CREATE);
753 if (aclresult != ACLCHECK_OK)
754 aclcheck_error(aclresult, OBJECT_SCHEMA,
755 get_namespace_name(namespaceId));
759 * Select tablespace to use. If not specified, use default tablespace
760 * (which may in turn default to database's default).
762 if (stmt->tableSpace)
764 tablespaceId = get_tablespace_oid(stmt->tableSpace, false);
765 if (partitioned && tablespaceId == MyDatabaseTableSpace)
766 ereport(ERROR,
767 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
768 errmsg("cannot specify default tablespace for partitioned relations")));
770 else
772 tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence,
773 partitioned);
774 /* note InvalidOid is OK in this case */
777 /* Check tablespace permissions */
778 if (check_rights &&
779 OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
781 AclResult aclresult;
783 aclresult = object_aclcheck(TableSpaceRelationId, tablespaceId, root_save_userid,
784 ACL_CREATE);
785 if (aclresult != ACLCHECK_OK)
786 aclcheck_error(aclresult, OBJECT_TABLESPACE,
787 get_tablespace_name(tablespaceId));
791 * Force shared indexes into the pg_global tablespace. This is a bit of a
792 * hack but seems simpler than marking them in the BKI commands. On the
793 * other hand, if it's not shared, don't allow it to be placed there.
795 if (rel->rd_rel->relisshared)
796 tablespaceId = GLOBALTABLESPACE_OID;
797 else if (tablespaceId == GLOBALTABLESPACE_OID)
798 ereport(ERROR,
799 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
800 errmsg("only shared relations can be placed in pg_global tablespace")));
803 * Choose the index column names.
805 indexColNames = ChooseIndexColumnNames(allIndexParams);
808 * Select name for index if caller didn't specify
810 indexRelationName = stmt->idxname;
811 if (indexRelationName == NULL)
812 indexRelationName = ChooseIndexName(RelationGetRelationName(rel),
813 namespaceId,
814 indexColNames,
815 stmt->excludeOpNames,
816 stmt->primary,
817 stmt->isconstraint);
820 * look up the access method, verify it can handle the requested features
822 accessMethodName = stmt->accessMethod;
823 tuple = SearchSysCache1(AMNAME, PointerGetDatum(accessMethodName));
824 if (!HeapTupleIsValid(tuple))
827 * Hack to provide more-or-less-transparent updating of old RTREE
828 * indexes to GiST: if RTREE is requested and not found, use GIST.
830 if (strcmp(accessMethodName, "rtree") == 0)
832 ereport(NOTICE,
833 (errmsg("substituting access method \"gist\" for obsolete method \"rtree\"")));
834 accessMethodName = "gist";
835 tuple = SearchSysCache1(AMNAME, PointerGetDatum(accessMethodName));
838 if (!HeapTupleIsValid(tuple))
839 ereport(ERROR,
840 (errcode(ERRCODE_UNDEFINED_OBJECT),
841 errmsg("access method \"%s\" does not exist",
842 accessMethodName)));
844 accessMethodForm = (Form_pg_am) GETSTRUCT(tuple);
845 accessMethodId = accessMethodForm->oid;
846 amRoutine = GetIndexAmRoutine(accessMethodForm->amhandler);
848 pgstat_progress_update_param(PROGRESS_CREATEIDX_ACCESS_METHOD_OID,
849 accessMethodId);
851 if (stmt->unique && !amRoutine->amcanunique)
852 ereport(ERROR,
853 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
854 errmsg("access method \"%s\" does not support unique indexes",
855 accessMethodName)));
856 if (stmt->indexIncludingParams != NIL && !amRoutine->amcaninclude)
857 ereport(ERROR,
858 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
859 errmsg("access method \"%s\" does not support included columns",
860 accessMethodName)));
861 if (numberOfKeyAttributes > 1 && !amRoutine->amcanmulticol)
862 ereport(ERROR,
863 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
864 errmsg("access method \"%s\" does not support multicolumn indexes",
865 accessMethodName)));
866 if (stmt->excludeOpNames && amRoutine->amgettuple == NULL)
867 ereport(ERROR,
868 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
869 errmsg("access method \"%s\" does not support exclusion constraints",
870 accessMethodName)));
872 amcanorder = amRoutine->amcanorder;
873 amoptions = amRoutine->amoptions;
874 amissummarizing = amRoutine->amsummarizing;
876 pfree(amRoutine);
877 ReleaseSysCache(tuple);
880 * Validate predicate, if given
882 if (stmt->whereClause)
883 CheckPredicate((Expr *) stmt->whereClause);
886 * Parse AM-specific options, convert to text array form, validate.
888 reloptions = transformRelOptions((Datum) 0, stmt->options,
889 NULL, NULL, false, false);
891 (void) index_reloptions(amoptions, reloptions, true);
894 * Prepare arguments for index_create, primarily an IndexInfo structure.
895 * Note that predicates must be in implicit-AND format. In a concurrent
896 * build, mark it not-ready-for-inserts.
898 indexInfo = makeIndexInfo(numberOfAttributes,
899 numberOfKeyAttributes,
900 accessMethodId,
901 NIL, /* expressions, NIL for now */
902 make_ands_implicit((Expr *) stmt->whereClause),
903 stmt->unique,
904 stmt->nulls_not_distinct,
905 !concurrent,
906 concurrent,
907 amissummarizing);
909 typeIds = palloc_array(Oid, numberOfAttributes);
910 collationIds = palloc_array(Oid, numberOfAttributes);
911 opclassIds = palloc_array(Oid, numberOfAttributes);
912 opclassOptions = palloc_array(Datum, numberOfAttributes);
913 coloptions = palloc_array(int16, numberOfAttributes);
914 ComputeIndexAttrs(indexInfo,
915 typeIds, collationIds, opclassIds, opclassOptions,
916 coloptions, allIndexParams,
917 stmt->excludeOpNames, tableId,
918 accessMethodName, accessMethodId,
919 amcanorder, stmt->isconstraint, root_save_userid,
920 root_save_sec_context, &root_save_nestlevel);
923 * Extra checks when creating a PRIMARY KEY index.
925 if (stmt->primary)
926 index_check_primary_key(rel, indexInfo, is_alter_table, stmt);
929 * If this table is partitioned and we're creating a unique index, primary
930 * key, or exclusion constraint, make sure that the partition key is a
931 * subset of the index's columns. Otherwise it would be possible to
932 * violate uniqueness by putting values that ought to be unique in
933 * different partitions.
935 * We could lift this limitation if we had global indexes, but those have
936 * their own problems, so this is a useful feature combination.
938 if (partitioned && (stmt->unique || stmt->excludeOpNames))
940 PartitionKey key = RelationGetPartitionKey(rel);
941 const char *constraint_type;
942 int i;
944 if (stmt->primary)
945 constraint_type = "PRIMARY KEY";
946 else if (stmt->unique)
947 constraint_type = "UNIQUE";
948 else if (stmt->excludeOpNames)
949 constraint_type = "EXCLUDE";
950 else
952 elog(ERROR, "unknown constraint type");
953 constraint_type = NULL; /* keep compiler quiet */
957 * Verify that all the columns in the partition key appear in the
958 * unique key definition, with the same notion of equality.
960 for (i = 0; i < key->partnatts; i++)
962 bool found = false;
963 int eq_strategy;
964 Oid ptkey_eqop;
965 int j;
968 * Identify the equality operator associated with this partkey
969 * column. For list and range partitioning, partkeys use btree
970 * operator classes; hash partitioning uses hash operator classes.
971 * (Keep this in sync with ComputePartitionAttrs!)
973 if (key->strategy == PARTITION_STRATEGY_HASH)
974 eq_strategy = HTEqualStrategyNumber;
975 else
976 eq_strategy = BTEqualStrategyNumber;
978 ptkey_eqop = get_opfamily_member(key->partopfamily[i],
979 key->partopcintype[i],
980 key->partopcintype[i],
981 eq_strategy);
982 if (!OidIsValid(ptkey_eqop))
983 elog(ERROR, "missing operator %d(%u,%u) in partition opfamily %u",
984 eq_strategy, key->partopcintype[i], key->partopcintype[i],
985 key->partopfamily[i]);
988 * We'll need to be able to identify the equality operators
989 * associated with index columns, too. We know what to do with
990 * btree opclasses; if there are ever any other index types that
991 * support unique indexes, this logic will need extension. But if
992 * we have an exclusion constraint, it already knows the
993 * operators, so we don't have to infer them.
995 if (stmt->unique && accessMethodId != BTREE_AM_OID)
996 ereport(ERROR,
997 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
998 errmsg("cannot match partition key to an index using access method \"%s\"",
999 accessMethodName)));
1002 * It may be possible to support UNIQUE constraints when partition
1003 * keys are expressions, but is it worth it? Give up for now.
1005 if (key->partattrs[i] == 0)
1006 ereport(ERROR,
1007 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1008 errmsg("unsupported %s constraint with partition key definition",
1009 constraint_type),
1010 errdetail("%s constraints cannot be used when partition keys include expressions.",
1011 constraint_type)));
1013 /* Search the index column(s) for a match */
1014 for (j = 0; j < indexInfo->ii_NumIndexKeyAttrs; j++)
1016 if (key->partattrs[i] == indexInfo->ii_IndexAttrNumbers[j])
1019 * Matched the column, now what about the collation and
1020 * equality op?
1022 Oid idx_opfamily;
1023 Oid idx_opcintype;
1025 if (key->partcollation[i] != collationIds[j])
1026 continue;
1028 if (get_opclass_opfamily_and_input_type(opclassIds[j],
1029 &idx_opfamily,
1030 &idx_opcintype))
1032 Oid idx_eqop = InvalidOid;
1034 if (stmt->unique)
1035 idx_eqop = get_opfamily_member(idx_opfamily,
1036 idx_opcintype,
1037 idx_opcintype,
1038 BTEqualStrategyNumber);
1039 else if (stmt->excludeOpNames)
1040 idx_eqop = indexInfo->ii_ExclusionOps[j];
1041 Assert(idx_eqop);
1043 if (ptkey_eqop == idx_eqop)
1045 found = true;
1046 break;
1048 else if (stmt->excludeOpNames)
1051 * We found a match, but it's not an equality
1052 * operator. Instead of failing below with an
1053 * error message about a missing column, fail now
1054 * and explain that the operator is wrong.
1056 Form_pg_attribute att = TupleDescAttr(RelationGetDescr(rel), key->partattrs[i] - 1);
1058 ereport(ERROR,
1059 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1060 errmsg("cannot match partition key to index on column \"%s\" using non-equal operator \"%s\"",
1061 NameStr(att->attname),
1062 get_opname(indexInfo->ii_ExclusionOps[j]))));
1068 if (!found)
1070 Form_pg_attribute att;
1072 att = TupleDescAttr(RelationGetDescr(rel),
1073 key->partattrs[i] - 1);
1074 ereport(ERROR,
1075 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1076 errmsg("unique constraint on partitioned table must include all partitioning columns"),
1077 errdetail("%s constraint on table \"%s\" lacks column \"%s\" which is part of the partition key.",
1078 constraint_type, RelationGetRelationName(rel),
1079 NameStr(att->attname))));
1086 * We disallow indexes on system columns. They would not necessarily get
1087 * updated correctly, and they don't seem useful anyway.
1089 for (int i = 0; i < indexInfo->ii_NumIndexAttrs; i++)
1091 AttrNumber attno = indexInfo->ii_IndexAttrNumbers[i];
1093 if (attno < 0)
1094 ereport(ERROR,
1095 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1096 errmsg("index creation on system columns is not supported")));
1100 * Also check for system columns used in expressions or predicates.
1102 if (indexInfo->ii_Expressions || indexInfo->ii_Predicate)
1104 Bitmapset *indexattrs = NULL;
1106 pull_varattnos((Node *) indexInfo->ii_Expressions, 1, &indexattrs);
1107 pull_varattnos((Node *) indexInfo->ii_Predicate, 1, &indexattrs);
1109 for (int i = FirstLowInvalidHeapAttributeNumber + 1; i < 0; i++)
1111 if (bms_is_member(i - FirstLowInvalidHeapAttributeNumber,
1112 indexattrs))
1113 ereport(ERROR,
1114 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1115 errmsg("index creation on system columns is not supported")));
1119 /* Is index safe for others to ignore? See set_indexsafe_procflags() */
1120 safe_index = indexInfo->ii_Expressions == NIL &&
1121 indexInfo->ii_Predicate == NIL;
1124 * Report index creation if appropriate (delay this till after most of the
1125 * error checks)
1127 if (stmt->isconstraint && !quiet)
1129 const char *constraint_type;
1131 if (stmt->primary)
1132 constraint_type = "PRIMARY KEY";
1133 else if (stmt->unique)
1134 constraint_type = "UNIQUE";
1135 else if (stmt->excludeOpNames)
1136 constraint_type = "EXCLUDE";
1137 else
1139 elog(ERROR, "unknown constraint type");
1140 constraint_type = NULL; /* keep compiler quiet */
1143 ereport(DEBUG1,
1144 (errmsg_internal("%s %s will create implicit index \"%s\" for table \"%s\"",
1145 is_alter_table ? "ALTER TABLE / ADD" : "CREATE TABLE /",
1146 constraint_type,
1147 indexRelationName, RelationGetRelationName(rel))));
1151 * A valid stmt->oldNumber implies that we already have a built form of
1152 * the index. The caller should also decline any index build.
1154 Assert(!RelFileNumberIsValid(stmt->oldNumber) || (skip_build && !concurrent));
1157 * Make the catalog entries for the index, including constraints. This
1158 * step also actually builds the index, except if caller requested not to
1159 * or in concurrent mode, in which case it'll be done later, or doing a
1160 * partitioned index (because those don't have storage).
1162 flags = constr_flags = 0;
1163 if (stmt->isconstraint)
1164 flags |= INDEX_CREATE_ADD_CONSTRAINT;
1165 if (skip_build || concurrent || partitioned)
1166 flags |= INDEX_CREATE_SKIP_BUILD;
1167 if (stmt->if_not_exists)
1168 flags |= INDEX_CREATE_IF_NOT_EXISTS;
1169 if (concurrent)
1170 flags |= INDEX_CREATE_CONCURRENT;
1171 if (partitioned)
1172 flags |= INDEX_CREATE_PARTITIONED;
1173 if (stmt->primary)
1174 flags |= INDEX_CREATE_IS_PRIMARY;
1177 * If the table is partitioned, and recursion was declined but partitions
1178 * exist, mark the index as invalid.
1180 if (partitioned && stmt->relation && !stmt->relation->inh)
1182 PartitionDesc pd = RelationGetPartitionDesc(rel, true);
1184 if (pd->nparts != 0)
1185 flags |= INDEX_CREATE_INVALID;
1188 if (stmt->deferrable)
1189 constr_flags |= INDEX_CONSTR_CREATE_DEFERRABLE;
1190 if (stmt->initdeferred)
1191 constr_flags |= INDEX_CONSTR_CREATE_INIT_DEFERRED;
1193 indexRelationId =
1194 index_create(rel, indexRelationName, indexRelationId, parentIndexId,
1195 parentConstraintId,
1196 stmt->oldNumber, indexInfo, indexColNames,
1197 accessMethodId, tablespaceId,
1198 collationIds, opclassIds, opclassOptions,
1199 coloptions, NULL, reloptions,
1200 flags, constr_flags,
1201 allowSystemTableMods, !check_rights,
1202 &createdConstraintId);
1204 ObjectAddressSet(address, RelationRelationId, indexRelationId);
1206 if (!OidIsValid(indexRelationId))
1209 * Roll back any GUC changes executed by index functions. Also revert
1210 * to original default_tablespace if we changed it above.
1212 AtEOXact_GUC(false, root_save_nestlevel);
1214 /* Restore userid and security context */
1215 SetUserIdAndSecContext(root_save_userid, root_save_sec_context);
1217 table_close(rel, NoLock);
1219 /* If this is the top-level index, we're done */
1220 if (!OidIsValid(parentIndexId))
1221 pgstat_progress_end_command();
1223 return address;
1227 * Roll back any GUC changes executed by index functions, and keep
1228 * subsequent changes local to this command. This is essential if some
1229 * index function changed a behavior-affecting GUC, e.g. search_path.
1231 AtEOXact_GUC(false, root_save_nestlevel);
1232 root_save_nestlevel = NewGUCNestLevel();
1233 RestrictSearchPath();
1235 /* Add any requested comment */
1236 if (stmt->idxcomment != NULL)
1237 CreateComments(indexRelationId, RelationRelationId, 0,
1238 stmt->idxcomment);
1240 if (partitioned)
1242 PartitionDesc partdesc;
1245 * Unless caller specified to skip this step (via ONLY), process each
1246 * partition to make sure they all contain a corresponding index.
1248 * If we're called internally (no stmt->relation), recurse always.
1250 partdesc = RelationGetPartitionDesc(rel, true);
1251 if ((!stmt->relation || stmt->relation->inh) && partdesc->nparts > 0)
1253 int nparts = partdesc->nparts;
1254 Oid *part_oids = palloc_array(Oid, nparts);
1255 bool invalidate_parent = false;
1256 Relation parentIndex;
1257 TupleDesc parentDesc;
1260 * Report the total number of partitions at the start of the
1261 * command; don't update it when being called recursively.
1263 if (!OidIsValid(parentIndexId))
1266 * When called by ProcessUtilitySlow, the number of partitions
1267 * is passed in as an optimization; but other callers pass -1
1268 * since they don't have the value handy. This should count
1269 * partitions the same way, ie one less than the number of
1270 * relations find_all_inheritors reports.
1272 * We assume we needn't ask find_all_inheritors to take locks,
1273 * because that should have happened already for all callers.
1274 * Even if it did not, this is safe as long as we don't try to
1275 * touch the partitions here; the worst consequence would be a
1276 * bogus progress-reporting total.
1278 if (total_parts < 0)
1280 List *children = find_all_inheritors(tableId, NoLock, NULL);
1282 total_parts = list_length(children) - 1;
1283 list_free(children);
1286 pgstat_progress_update_param(PROGRESS_CREATEIDX_PARTITIONS_TOTAL,
1287 total_parts);
1290 /* Make a local copy of partdesc->oids[], just for safety */
1291 memcpy(part_oids, partdesc->oids, sizeof(Oid) * nparts);
1294 * We'll need an IndexInfo describing the parent index. The one
1295 * built above is almost good enough, but not quite, because (for
1296 * example) its predicate expression if any hasn't been through
1297 * expression preprocessing. The most reliable way to get an
1298 * IndexInfo that will match those for child indexes is to build
1299 * it the same way, using BuildIndexInfo().
1301 parentIndex = index_open(indexRelationId, lockmode);
1302 indexInfo = BuildIndexInfo(parentIndex);
1304 parentDesc = RelationGetDescr(rel);
1307 * For each partition, scan all existing indexes; if one matches
1308 * our index definition and is not already attached to some other
1309 * parent index, attach it to the one we just created.
1311 * If none matches, build a new index by calling ourselves
1312 * recursively with the same options (except for the index name).
1314 for (int i = 0; i < nparts; i++)
1316 Oid childRelid = part_oids[i];
1317 Relation childrel;
1318 Oid child_save_userid;
1319 int child_save_sec_context;
1320 int child_save_nestlevel;
1321 List *childidxs;
1322 ListCell *cell;
1323 AttrMap *attmap;
1324 bool found = false;
1326 childrel = table_open(childRelid, lockmode);
1328 GetUserIdAndSecContext(&child_save_userid,
1329 &child_save_sec_context);
1330 SetUserIdAndSecContext(childrel->rd_rel->relowner,
1331 child_save_sec_context | SECURITY_RESTRICTED_OPERATION);
1332 child_save_nestlevel = NewGUCNestLevel();
1333 RestrictSearchPath();
1336 * Don't try to create indexes on foreign tables, though. Skip
1337 * those if a regular index, or fail if trying to create a
1338 * constraint index.
1340 if (childrel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
1342 if (stmt->unique || stmt->primary)
1343 ereport(ERROR,
1344 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1345 errmsg("cannot create unique index on partitioned table \"%s\"",
1346 RelationGetRelationName(rel)),
1347 errdetail("Table \"%s\" contains partitions that are foreign tables.",
1348 RelationGetRelationName(rel))));
1350 AtEOXact_GUC(false, child_save_nestlevel);
1351 SetUserIdAndSecContext(child_save_userid,
1352 child_save_sec_context);
1353 table_close(childrel, lockmode);
1354 continue;
1357 childidxs = RelationGetIndexList(childrel);
1358 attmap =
1359 build_attrmap_by_name(RelationGetDescr(childrel),
1360 parentDesc,
1361 false);
1363 foreach(cell, childidxs)
1365 Oid cldidxid = lfirst_oid(cell);
1366 Relation cldidx;
1367 IndexInfo *cldIdxInfo;
1369 /* this index is already partition of another one */
1370 if (has_superclass(cldidxid))
1371 continue;
1373 cldidx = index_open(cldidxid, lockmode);
1374 cldIdxInfo = BuildIndexInfo(cldidx);
1375 if (CompareIndexInfo(cldIdxInfo, indexInfo,
1376 cldidx->rd_indcollation,
1377 parentIndex->rd_indcollation,
1378 cldidx->rd_opfamily,
1379 parentIndex->rd_opfamily,
1380 attmap))
1382 Oid cldConstrOid = InvalidOid;
1385 * Found a match.
1387 * If this index is being created in the parent
1388 * because of a constraint, then the child needs to
1389 * have a constraint also, so look for one. If there
1390 * is no such constraint, this index is no good, so
1391 * keep looking.
1393 if (createdConstraintId != InvalidOid)
1395 cldConstrOid =
1396 get_relation_idx_constraint_oid(childRelid,
1397 cldidxid);
1398 if (cldConstrOid == InvalidOid)
1400 index_close(cldidx, lockmode);
1401 continue;
1405 /* Attach index to parent and we're done. */
1406 IndexSetParentIndex(cldidx, indexRelationId);
1407 if (createdConstraintId != InvalidOid)
1408 ConstraintSetParentConstraint(cldConstrOid,
1409 createdConstraintId,
1410 childRelid);
1412 if (!cldidx->rd_index->indisvalid)
1413 invalidate_parent = true;
1415 found = true;
1418 * Report this partition as processed. Note that if
1419 * the partition has children itself, we'd ideally
1420 * count the children and update the progress report
1421 * for all of them; but that seems unduly expensive.
1422 * Instead, the progress report will act like all such
1423 * indirect children were processed in zero time at
1424 * the end of the command.
1426 pgstat_progress_incr_param(PROGRESS_CREATEIDX_PARTITIONS_DONE, 1);
1428 /* keep lock till commit */
1429 index_close(cldidx, NoLock);
1430 break;
1433 index_close(cldidx, lockmode);
1436 list_free(childidxs);
1437 AtEOXact_GUC(false, child_save_nestlevel);
1438 SetUserIdAndSecContext(child_save_userid,
1439 child_save_sec_context);
1440 table_close(childrel, NoLock);
1443 * If no matching index was found, create our own.
1445 if (!found)
1447 IndexStmt *childStmt = copyObject(stmt);
1448 bool found_whole_row;
1449 ListCell *lc;
1450 ObjectAddress childAddr;
1453 * We can't use the same index name for the child index,
1454 * so clear idxname to let the recursive invocation choose
1455 * a new name. Likewise, the existing target relation
1456 * field is wrong, and if indexOid or oldNumber are set,
1457 * they mustn't be applied to the child either.
1459 childStmt->idxname = NULL;
1460 childStmt->relation = NULL;
1461 childStmt->indexOid = InvalidOid;
1462 childStmt->oldNumber = InvalidRelFileNumber;
1463 childStmt->oldCreateSubid = InvalidSubTransactionId;
1464 childStmt->oldFirstRelfilelocatorSubid = InvalidSubTransactionId;
1467 * Adjust any Vars (both in expressions and in the index's
1468 * WHERE clause) to match the partition's column numbering
1469 * in case it's different from the parent's.
1471 foreach(lc, childStmt->indexParams)
1473 IndexElem *ielem = lfirst(lc);
1476 * If the index parameter is an expression, we must
1477 * translate it to contain child Vars.
1479 if (ielem->expr)
1481 ielem->expr =
1482 map_variable_attnos((Node *) ielem->expr,
1483 1, 0, attmap,
1484 InvalidOid,
1485 &found_whole_row);
1486 if (found_whole_row)
1487 elog(ERROR, "cannot convert whole-row table reference");
1490 childStmt->whereClause =
1491 map_variable_attnos(stmt->whereClause, 1, 0,
1492 attmap,
1493 InvalidOid, &found_whole_row);
1494 if (found_whole_row)
1495 elog(ERROR, "cannot convert whole-row table reference");
1498 * Recurse as the starting user ID. Callee will use that
1499 * for permission checks, then switch again.
1501 Assert(GetUserId() == child_save_userid);
1502 SetUserIdAndSecContext(root_save_userid,
1503 root_save_sec_context);
1504 childAddr =
1505 DefineIndex(childRelid, childStmt,
1506 InvalidOid, /* no predefined OID */
1507 indexRelationId, /* this is our child */
1508 createdConstraintId,
1510 is_alter_table, check_rights,
1511 check_not_in_use,
1512 skip_build, quiet);
1513 SetUserIdAndSecContext(child_save_userid,
1514 child_save_sec_context);
1517 * Check if the index just created is valid or not, as it
1518 * could be possible that it has been switched as invalid
1519 * when recursing across multiple partition levels.
1521 if (!get_index_isvalid(childAddr.objectId))
1522 invalidate_parent = true;
1525 free_attrmap(attmap);
1528 index_close(parentIndex, lockmode);
1531 * The pg_index row we inserted for this index was marked
1532 * indisvalid=true. But if we attached an existing index that is
1533 * invalid, this is incorrect, so update our row to invalid too.
1535 if (invalidate_parent)
1537 Relation pg_index = table_open(IndexRelationId, RowExclusiveLock);
1538 HeapTuple tup,
1539 newtup;
1541 tup = SearchSysCache1(INDEXRELID,
1542 ObjectIdGetDatum(indexRelationId));
1543 if (!HeapTupleIsValid(tup))
1544 elog(ERROR, "cache lookup failed for index %u",
1545 indexRelationId);
1546 newtup = heap_copytuple(tup);
1547 ((Form_pg_index) GETSTRUCT(newtup))->indisvalid = false;
1548 CatalogTupleUpdate(pg_index, &tup->t_self, newtup);
1549 ReleaseSysCache(tup);
1550 table_close(pg_index, RowExclusiveLock);
1551 heap_freetuple(newtup);
1554 * CCI here to make this update visible, in case this recurses
1555 * across multiple partition levels.
1557 CommandCounterIncrement();
1562 * Indexes on partitioned tables are not themselves built, so we're
1563 * done here.
1565 AtEOXact_GUC(false, root_save_nestlevel);
1566 SetUserIdAndSecContext(root_save_userid, root_save_sec_context);
1567 table_close(rel, NoLock);
1568 if (!OidIsValid(parentIndexId))
1569 pgstat_progress_end_command();
1570 else
1572 /* Update progress for an intermediate partitioned index itself */
1573 pgstat_progress_incr_param(PROGRESS_CREATEIDX_PARTITIONS_DONE, 1);
1576 return address;
1579 AtEOXact_GUC(false, root_save_nestlevel);
1580 SetUserIdAndSecContext(root_save_userid, root_save_sec_context);
1582 if (!concurrent)
1584 /* Close the heap and we're done, in the non-concurrent case */
1585 table_close(rel, NoLock);
1588 * If this is the top-level index, the command is done overall;
1589 * otherwise, increment progress to report one child index is done.
1591 if (!OidIsValid(parentIndexId))
1592 pgstat_progress_end_command();
1593 else
1594 pgstat_progress_incr_param(PROGRESS_CREATEIDX_PARTITIONS_DONE, 1);
1596 return address;
1599 /* save lockrelid and locktag for below, then close rel */
1600 heaprelid = rel->rd_lockInfo.lockRelId;
1601 SET_LOCKTAG_RELATION(heaplocktag, heaprelid.dbId, heaprelid.relId);
1602 table_close(rel, NoLock);
1605 * For a concurrent build, it's important to make the catalog entries
1606 * visible to other transactions before we start to build the index. That
1607 * will prevent them from making incompatible HOT updates. The new index
1608 * will be marked not indisready and not indisvalid, so that no one else
1609 * tries to either insert into it or use it for queries.
1611 * We must commit our current transaction so that the index becomes
1612 * visible; then start another. Note that all the data structures we just
1613 * built are lost in the commit. The only data we keep past here are the
1614 * relation IDs.
1616 * Before committing, get a session-level lock on the table, to ensure
1617 * that neither it nor the index can be dropped before we finish. This
1618 * cannot block, even if someone else is waiting for access, because we
1619 * already have the same lock within our transaction.
1621 * Note: we don't currently bother with a session lock on the index,
1622 * because there are no operations that could change its state while we
1623 * hold lock on the parent table. This might need to change later.
1625 LockRelationIdForSession(&heaprelid, ShareUpdateExclusiveLock);
1627 PopActiveSnapshot();
1628 CommitTransactionCommand();
1629 StartTransactionCommand();
1631 /* Tell concurrent index builds to ignore us, if index qualifies */
1632 if (safe_index)
1633 set_indexsafe_procflags();
1636 * The index is now visible, so we can report the OID. While on it,
1637 * include the report for the beginning of phase 2.
1640 const int progress_cols[] = {
1641 PROGRESS_CREATEIDX_INDEX_OID,
1642 PROGRESS_CREATEIDX_PHASE
1644 const int64 progress_vals[] = {
1645 indexRelationId,
1646 PROGRESS_CREATEIDX_PHASE_WAIT_1
1649 pgstat_progress_update_multi_param(2, progress_cols, progress_vals);
1653 * Phase 2 of concurrent index build (see comments for validate_index()
1654 * for an overview of how this works)
1656 * Now we must wait until no running transaction could have the table open
1657 * with the old list of indexes. Use ShareLock to consider running
1658 * transactions that hold locks that permit writing to the table. Note we
1659 * do not need to worry about xacts that open the table for writing after
1660 * this point; they will see the new index when they open it.
1662 * Note: the reason we use actual lock acquisition here, rather than just
1663 * checking the ProcArray and sleeping, is that deadlock is possible if
1664 * one of the transactions in question is blocked trying to acquire an
1665 * exclusive lock on our table. The lock code will detect deadlock and
1666 * error out properly.
1668 WaitForLockers(heaplocktag, ShareLock, true);
1671 * At this moment we are sure that there are no transactions with the
1672 * table open for write that don't have this new index in their list of
1673 * indexes. We have waited out all the existing transactions and any new
1674 * transaction will have the new index in its list, but the index is still
1675 * marked as "not-ready-for-inserts". The index is consulted while
1676 * deciding HOT-safety though. This arrangement ensures that no new HOT
1677 * chains can be created where the new tuple and the old tuple in the
1678 * chain have different index keys.
1680 * We now take a new snapshot, and build the index using all tuples that
1681 * are visible in this snapshot. We can be sure that any HOT updates to
1682 * these tuples will be compatible with the index, since any updates made
1683 * by transactions that didn't know about the index are now committed or
1684 * rolled back. Thus, each visible tuple is either the end of its
1685 * HOT-chain or the extension of the chain is HOT-safe for this index.
1688 /* Set ActiveSnapshot since functions in the indexes may need it */
1689 PushActiveSnapshot(GetTransactionSnapshot());
1691 /* Perform concurrent build of index */
1692 index_concurrently_build(tableId, indexRelationId);
1694 /* we can do away with our snapshot */
1695 PopActiveSnapshot();
1698 * Commit this transaction to make the indisready update visible.
1700 CommitTransactionCommand();
1701 StartTransactionCommand();
1703 /* Tell concurrent index builds to ignore us, if index qualifies */
1704 if (safe_index)
1705 set_indexsafe_procflags();
1708 * Phase 3 of concurrent index build
1710 * We once again wait until no transaction can have the table open with
1711 * the index marked as read-only for updates.
1713 pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE,
1714 PROGRESS_CREATEIDX_PHASE_WAIT_2);
1715 WaitForLockers(heaplocktag, ShareLock, true);
1718 * Now take the "reference snapshot" that will be used by validate_index()
1719 * to filter candidate tuples. Beware! There might still be snapshots in
1720 * use that treat some transaction as in-progress that our reference
1721 * snapshot treats as committed. If such a recently-committed transaction
1722 * deleted tuples in the table, we will not include them in the index; yet
1723 * those transactions which see the deleting one as still-in-progress will
1724 * expect such tuples to be there once we mark the index as valid.
1726 * We solve this by waiting for all endangered transactions to exit before
1727 * we mark the index as valid.
1729 * We also set ActiveSnapshot to this snap, since functions in indexes may
1730 * need a snapshot.
1732 snapshot = RegisterSnapshot(GetTransactionSnapshot());
1733 PushActiveSnapshot(snapshot);
1736 * Scan the index and the heap, insert any missing index entries.
1738 validate_index(tableId, indexRelationId, snapshot);
1741 * Drop the reference snapshot. We must do this before waiting out other
1742 * snapshot holders, else we will deadlock against other processes also
1743 * doing CREATE INDEX CONCURRENTLY, which would see our snapshot as one
1744 * they must wait for. But first, save the snapshot's xmin to use as
1745 * limitXmin for GetCurrentVirtualXIDs().
1747 limitXmin = snapshot->xmin;
1749 PopActiveSnapshot();
1750 UnregisterSnapshot(snapshot);
1753 * The snapshot subsystem could still contain registered snapshots that
1754 * are holding back our process's advertised xmin; in particular, if
1755 * default_transaction_isolation = serializable, there is a transaction
1756 * snapshot that is still active. The CatalogSnapshot is likewise a
1757 * hazard. To ensure no deadlocks, we must commit and start yet another
1758 * transaction, and do our wait before any snapshot has been taken in it.
1760 CommitTransactionCommand();
1761 StartTransactionCommand();
1763 /* Tell concurrent index builds to ignore us, if index qualifies */
1764 if (safe_index)
1765 set_indexsafe_procflags();
1767 /* We should now definitely not be advertising any xmin. */
1768 Assert(MyProc->xmin == InvalidTransactionId);
1771 * The index is now valid in the sense that it contains all currently
1772 * interesting tuples. But since it might not contain tuples deleted just
1773 * before the reference snap was taken, we have to wait out any
1774 * transactions that might have older snapshots.
1776 pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE,
1777 PROGRESS_CREATEIDX_PHASE_WAIT_3);
1778 WaitForOlderSnapshots(limitXmin, true);
1781 * Index can now be marked valid -- update its pg_index entry
1783 index_set_state_flags(indexRelationId, INDEX_CREATE_SET_VALID);
1786 * The pg_index update will cause backends (including this one) to update
1787 * relcache entries for the index itself, but we should also send a
1788 * relcache inval on the parent table to force replanning of cached plans.
1789 * Otherwise existing sessions might fail to use the new index where it
1790 * would be useful. (Note that our earlier commits did not create reasons
1791 * to replan; so relcache flush on the index itself was sufficient.)
1793 CacheInvalidateRelcacheByRelid(heaprelid.relId);
1796 * Last thing to do is release the session-level lock on the parent table.
1798 UnlockRelationIdForSession(&heaprelid, ShareUpdateExclusiveLock);
1800 pgstat_progress_end_command();
1802 return address;
1807 * CheckPredicate
1808 * Checks that the given partial-index predicate is valid.
1810 * This used to also constrain the form of the predicate to forms that
1811 * indxpath.c could do something with. However, that seems overly
1812 * restrictive. One useful application of partial indexes is to apply
1813 * a UNIQUE constraint across a subset of a table, and in that scenario
1814 * any evaluable predicate will work. So accept any predicate here
1815 * (except ones requiring a plan), and let indxpath.c fend for itself.
1817 static void
1818 CheckPredicate(Expr *predicate)
1821 * transformExpr() should have already rejected subqueries, aggregates,
1822 * and window functions, based on the EXPR_KIND_ for a predicate.
1826 * A predicate using mutable functions is probably wrong, for the same
1827 * reasons that we don't allow an index expression to use one.
1829 if (contain_mutable_functions_after_planning(predicate))
1830 ereport(ERROR,
1831 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1832 errmsg("functions in index predicate must be marked IMMUTABLE")));
1836 * Compute per-index-column information, including indexed column numbers
1837 * or index expressions, opclasses and their options. Note, all output vectors
1838 * should be allocated for all columns, including "including" ones.
1840 * If the caller switched to the table owner, ddl_userid is the role for ACL
1841 * checks reached without traversing opaque expressions. Otherwise, it's
1842 * InvalidOid, and other ddl_* arguments are undefined.
1844 static void
1845 ComputeIndexAttrs(IndexInfo *indexInfo,
1846 Oid *typeOids,
1847 Oid *collationOids,
1848 Oid *opclassOids,
1849 Datum *opclassOptions,
1850 int16 *colOptions,
1851 const List *attList, /* list of IndexElem's */
1852 const List *exclusionOpNames,
1853 Oid relId,
1854 const char *accessMethodName,
1855 Oid accessMethodId,
1856 bool amcanorder,
1857 bool isconstraint,
1858 Oid ddl_userid,
1859 int ddl_sec_context,
1860 int *ddl_save_nestlevel)
1862 ListCell *nextExclOp;
1863 ListCell *lc;
1864 int attn;
1865 int nkeycols = indexInfo->ii_NumIndexKeyAttrs;
1866 Oid save_userid;
1867 int save_sec_context;
1869 /* Allocate space for exclusion operator info, if needed */
1870 if (exclusionOpNames)
1872 Assert(list_length(exclusionOpNames) == nkeycols);
1873 indexInfo->ii_ExclusionOps = palloc_array(Oid, nkeycols);
1874 indexInfo->ii_ExclusionProcs = palloc_array(Oid, nkeycols);
1875 indexInfo->ii_ExclusionStrats = palloc_array(uint16, nkeycols);
1876 nextExclOp = list_head(exclusionOpNames);
1878 else
1879 nextExclOp = NULL;
1881 if (OidIsValid(ddl_userid))
1882 GetUserIdAndSecContext(&save_userid, &save_sec_context);
1885 * process attributeList
1887 attn = 0;
1888 foreach(lc, attList)
1890 IndexElem *attribute = (IndexElem *) lfirst(lc);
1891 Oid atttype;
1892 Oid attcollation;
1895 * Process the column-or-expression to be indexed.
1897 if (attribute->name != NULL)
1899 /* Simple index attribute */
1900 HeapTuple atttuple;
1901 Form_pg_attribute attform;
1903 Assert(attribute->expr == NULL);
1904 atttuple = SearchSysCacheAttName(relId, attribute->name);
1905 if (!HeapTupleIsValid(atttuple))
1907 /* difference in error message spellings is historical */
1908 if (isconstraint)
1909 ereport(ERROR,
1910 (errcode(ERRCODE_UNDEFINED_COLUMN),
1911 errmsg("column \"%s\" named in key does not exist",
1912 attribute->name)));
1913 else
1914 ereport(ERROR,
1915 (errcode(ERRCODE_UNDEFINED_COLUMN),
1916 errmsg("column \"%s\" does not exist",
1917 attribute->name)));
1919 attform = (Form_pg_attribute) GETSTRUCT(atttuple);
1920 indexInfo->ii_IndexAttrNumbers[attn] = attform->attnum;
1921 atttype = attform->atttypid;
1922 attcollation = attform->attcollation;
1923 ReleaseSysCache(atttuple);
1925 else
1927 /* Index expression */
1928 Node *expr = attribute->expr;
1930 Assert(expr != NULL);
1932 if (attn >= nkeycols)
1933 ereport(ERROR,
1934 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1935 errmsg("expressions are not supported in included columns")));
1936 atttype = exprType(expr);
1937 attcollation = exprCollation(expr);
1940 * Strip any top-level COLLATE clause. This ensures that we treat
1941 * "x COLLATE y" and "(x COLLATE y)" alike.
1943 while (IsA(expr, CollateExpr))
1944 expr = (Node *) ((CollateExpr *) expr)->arg;
1946 if (IsA(expr, Var) &&
1947 ((Var *) expr)->varattno != InvalidAttrNumber)
1950 * User wrote "(column)" or "(column COLLATE something)".
1951 * Treat it like simple attribute anyway.
1953 indexInfo->ii_IndexAttrNumbers[attn] = ((Var *) expr)->varattno;
1955 else
1957 indexInfo->ii_IndexAttrNumbers[attn] = 0; /* marks expression */
1958 indexInfo->ii_Expressions = lappend(indexInfo->ii_Expressions,
1959 expr);
1962 * transformExpr() should have already rejected subqueries,
1963 * aggregates, and window functions, based on the EXPR_KIND_
1964 * for an index expression.
1968 * An expression using mutable functions is probably wrong,
1969 * since if you aren't going to get the same result for the
1970 * same data every time, it's not clear what the index entries
1971 * mean at all.
1973 if (contain_mutable_functions_after_planning((Expr *) expr))
1974 ereport(ERROR,
1975 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1976 errmsg("functions in index expression must be marked IMMUTABLE")));
1980 typeOids[attn] = atttype;
1983 * Included columns have no collation, no opclass and no ordering
1984 * options.
1986 if (attn >= nkeycols)
1988 if (attribute->collation)
1989 ereport(ERROR,
1990 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1991 errmsg("including column does not support a collation")));
1992 if (attribute->opclass)
1993 ereport(ERROR,
1994 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1995 errmsg("including column does not support an operator class")));
1996 if (attribute->ordering != SORTBY_DEFAULT)
1997 ereport(ERROR,
1998 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1999 errmsg("including column does not support ASC/DESC options")));
2000 if (attribute->nulls_ordering != SORTBY_NULLS_DEFAULT)
2001 ereport(ERROR,
2002 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2003 errmsg("including column does not support NULLS FIRST/LAST options")));
2005 opclassOids[attn] = InvalidOid;
2006 opclassOptions[attn] = (Datum) 0;
2007 colOptions[attn] = 0;
2008 collationOids[attn] = InvalidOid;
2009 attn++;
2011 continue;
2015 * Apply collation override if any. Use of ddl_userid is necessary
2016 * due to ACL checks therein, and it's safe because collations don't
2017 * contain opaque expressions (or non-opaque expressions).
2019 if (attribute->collation)
2021 if (OidIsValid(ddl_userid))
2023 AtEOXact_GUC(false, *ddl_save_nestlevel);
2024 SetUserIdAndSecContext(ddl_userid, ddl_sec_context);
2026 attcollation = get_collation_oid(attribute->collation, false);
2027 if (OidIsValid(ddl_userid))
2029 SetUserIdAndSecContext(save_userid, save_sec_context);
2030 *ddl_save_nestlevel = NewGUCNestLevel();
2031 RestrictSearchPath();
2036 * Check we have a collation iff it's a collatable type. The only
2037 * expected failures here are (1) COLLATE applied to a noncollatable
2038 * type, or (2) index expression had an unresolved collation. But we
2039 * might as well code this to be a complete consistency check.
2041 if (type_is_collatable(atttype))
2043 if (!OidIsValid(attcollation))
2044 ereport(ERROR,
2045 (errcode(ERRCODE_INDETERMINATE_COLLATION),
2046 errmsg("could not determine which collation to use for index expression"),
2047 errhint("Use the COLLATE clause to set the collation explicitly.")));
2049 else
2051 if (OidIsValid(attcollation))
2052 ereport(ERROR,
2053 (errcode(ERRCODE_DATATYPE_MISMATCH),
2054 errmsg("collations are not supported by type %s",
2055 format_type_be(atttype))));
2058 collationOids[attn] = attcollation;
2061 * Identify the opclass to use. Use of ddl_userid is necessary due to
2062 * ACL checks therein. This is safe despite opclasses containing
2063 * opaque expressions (specifically, functions), because only
2064 * superusers can define opclasses.
2066 if (OidIsValid(ddl_userid))
2068 AtEOXact_GUC(false, *ddl_save_nestlevel);
2069 SetUserIdAndSecContext(ddl_userid, ddl_sec_context);
2071 opclassOids[attn] = ResolveOpClass(attribute->opclass,
2072 atttype,
2073 accessMethodName,
2074 accessMethodId);
2075 if (OidIsValid(ddl_userid))
2077 SetUserIdAndSecContext(save_userid, save_sec_context);
2078 *ddl_save_nestlevel = NewGUCNestLevel();
2079 RestrictSearchPath();
2083 * Identify the exclusion operator, if any.
2085 if (nextExclOp)
2087 List *opname = (List *) lfirst(nextExclOp);
2088 Oid opid;
2089 Oid opfamily;
2090 int strat;
2093 * Find the operator --- it must accept the column datatype
2094 * without runtime coercion (but binary compatibility is OK).
2095 * Operators contain opaque expressions (specifically, functions).
2096 * compatible_oper_opid() boils down to oper() and
2097 * IsBinaryCoercible(). PostgreSQL would have security problems
2098 * elsewhere if oper() started calling opaque expressions.
2100 if (OidIsValid(ddl_userid))
2102 AtEOXact_GUC(false, *ddl_save_nestlevel);
2103 SetUserIdAndSecContext(ddl_userid, ddl_sec_context);
2105 opid = compatible_oper_opid(opname, atttype, atttype, false);
2106 if (OidIsValid(ddl_userid))
2108 SetUserIdAndSecContext(save_userid, save_sec_context);
2109 *ddl_save_nestlevel = NewGUCNestLevel();
2110 RestrictSearchPath();
2114 * Only allow commutative operators to be used in exclusion
2115 * constraints. If X conflicts with Y, but Y does not conflict
2116 * with X, bad things will happen.
2118 if (get_commutator(opid) != opid)
2119 ereport(ERROR,
2120 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2121 errmsg("operator %s is not commutative",
2122 format_operator(opid)),
2123 errdetail("Only commutative operators can be used in exclusion constraints.")));
2126 * Operator must be a member of the right opfamily, too
2128 opfamily = get_opclass_family(opclassOids[attn]);
2129 strat = get_op_opfamily_strategy(opid, opfamily);
2130 if (strat == 0)
2132 HeapTuple opftuple;
2133 Form_pg_opfamily opfform;
2136 * attribute->opclass might not explicitly name the opfamily,
2137 * so fetch the name of the selected opfamily for use in the
2138 * error message.
2140 opftuple = SearchSysCache1(OPFAMILYOID,
2141 ObjectIdGetDatum(opfamily));
2142 if (!HeapTupleIsValid(opftuple))
2143 elog(ERROR, "cache lookup failed for opfamily %u",
2144 opfamily);
2145 opfform = (Form_pg_opfamily) GETSTRUCT(opftuple);
2147 ereport(ERROR,
2148 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2149 errmsg("operator %s is not a member of operator family \"%s\"",
2150 format_operator(opid),
2151 NameStr(opfform->opfname)),
2152 errdetail("The exclusion operator must be related to the index operator class for the constraint.")));
2155 indexInfo->ii_ExclusionOps[attn] = opid;
2156 indexInfo->ii_ExclusionProcs[attn] = get_opcode(opid);
2157 indexInfo->ii_ExclusionStrats[attn] = strat;
2158 nextExclOp = lnext(exclusionOpNames, nextExclOp);
2162 * Set up the per-column options (indoption field). For now, this is
2163 * zero for any un-ordered index, while ordered indexes have DESC and
2164 * NULLS FIRST/LAST options.
2166 colOptions[attn] = 0;
2167 if (amcanorder)
2169 /* default ordering is ASC */
2170 if (attribute->ordering == SORTBY_DESC)
2171 colOptions[attn] |= INDOPTION_DESC;
2172 /* default null ordering is LAST for ASC, FIRST for DESC */
2173 if (attribute->nulls_ordering == SORTBY_NULLS_DEFAULT)
2175 if (attribute->ordering == SORTBY_DESC)
2176 colOptions[attn] |= INDOPTION_NULLS_FIRST;
2178 else if (attribute->nulls_ordering == SORTBY_NULLS_FIRST)
2179 colOptions[attn] |= INDOPTION_NULLS_FIRST;
2181 else
2183 /* index AM does not support ordering */
2184 if (attribute->ordering != SORTBY_DEFAULT)
2185 ereport(ERROR,
2186 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2187 errmsg("access method \"%s\" does not support ASC/DESC options",
2188 accessMethodName)));
2189 if (attribute->nulls_ordering != SORTBY_NULLS_DEFAULT)
2190 ereport(ERROR,
2191 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2192 errmsg("access method \"%s\" does not support NULLS FIRST/LAST options",
2193 accessMethodName)));
2196 /* Set up the per-column opclass options (attoptions field). */
2197 if (attribute->opclassopts)
2199 Assert(attn < nkeycols);
2201 opclassOptions[attn] =
2202 transformRelOptions((Datum) 0, attribute->opclassopts,
2203 NULL, NULL, false, false);
2205 else
2206 opclassOptions[attn] = (Datum) 0;
2208 attn++;
2213 * Resolve possibly-defaulted operator class specification
2215 * Note: This is used to resolve operator class specifications in index and
2216 * partition key definitions.
2219 ResolveOpClass(const List *opclass, Oid attrType,
2220 const char *accessMethodName, Oid accessMethodId)
2222 char *schemaname;
2223 char *opcname;
2224 HeapTuple tuple;
2225 Form_pg_opclass opform;
2226 Oid opClassId,
2227 opInputType;
2229 if (opclass == NIL)
2231 /* no operator class specified, so find the default */
2232 opClassId = GetDefaultOpClass(attrType, accessMethodId);
2233 if (!OidIsValid(opClassId))
2234 ereport(ERROR,
2235 (errcode(ERRCODE_UNDEFINED_OBJECT),
2236 errmsg("data type %s has no default operator class for access method \"%s\"",
2237 format_type_be(attrType), accessMethodName),
2238 errhint("You must specify an operator class for the index or define a default operator class for the data type.")));
2239 return opClassId;
2243 * Specific opclass name given, so look up the opclass.
2246 /* deconstruct the name list */
2247 DeconstructQualifiedName(opclass, &schemaname, &opcname);
2249 if (schemaname)
2251 /* Look in specific schema only */
2252 Oid namespaceId;
2254 namespaceId = LookupExplicitNamespace(schemaname, false);
2255 tuple = SearchSysCache3(CLAAMNAMENSP,
2256 ObjectIdGetDatum(accessMethodId),
2257 PointerGetDatum(opcname),
2258 ObjectIdGetDatum(namespaceId));
2260 else
2262 /* Unqualified opclass name, so search the search path */
2263 opClassId = OpclassnameGetOpcid(accessMethodId, opcname);
2264 if (!OidIsValid(opClassId))
2265 ereport(ERROR,
2266 (errcode(ERRCODE_UNDEFINED_OBJECT),
2267 errmsg("operator class \"%s\" does not exist for access method \"%s\"",
2268 opcname, accessMethodName)));
2269 tuple = SearchSysCache1(CLAOID, ObjectIdGetDatum(opClassId));
2272 if (!HeapTupleIsValid(tuple))
2273 ereport(ERROR,
2274 (errcode(ERRCODE_UNDEFINED_OBJECT),
2275 errmsg("operator class \"%s\" does not exist for access method \"%s\"",
2276 NameListToString(opclass), accessMethodName)));
2279 * Verify that the index operator class accepts this datatype. Note we
2280 * will accept binary compatibility.
2282 opform = (Form_pg_opclass) GETSTRUCT(tuple);
2283 opClassId = opform->oid;
2284 opInputType = opform->opcintype;
2286 if (!IsBinaryCoercible(attrType, opInputType))
2287 ereport(ERROR,
2288 (errcode(ERRCODE_DATATYPE_MISMATCH),
2289 errmsg("operator class \"%s\" does not accept data type %s",
2290 NameListToString(opclass), format_type_be(attrType))));
2292 ReleaseSysCache(tuple);
2294 return opClassId;
2298 * GetDefaultOpClass
2300 * Given the OIDs of a datatype and an access method, find the default
2301 * operator class, if any. Returns InvalidOid if there is none.
2304 GetDefaultOpClass(Oid type_id, Oid am_id)
2306 Oid result = InvalidOid;
2307 int nexact = 0;
2308 int ncompatible = 0;
2309 int ncompatiblepreferred = 0;
2310 Relation rel;
2311 ScanKeyData skey[1];
2312 SysScanDesc scan;
2313 HeapTuple tup;
2314 TYPCATEGORY tcategory;
2316 /* If it's a domain, look at the base type instead */
2317 type_id = getBaseType(type_id);
2319 tcategory = TypeCategory(type_id);
2322 * We scan through all the opclasses available for the access method,
2323 * looking for one that is marked default and matches the target type
2324 * (either exactly or binary-compatibly, but prefer an exact match).
2326 * We could find more than one binary-compatible match. If just one is
2327 * for a preferred type, use that one; otherwise we fail, forcing the user
2328 * to specify which one he wants. (The preferred-type special case is a
2329 * kluge for varchar: it's binary-compatible to both text and bpchar, so
2330 * we need a tiebreaker.) If we find more than one exact match, then
2331 * someone put bogus entries in pg_opclass.
2333 rel = table_open(OperatorClassRelationId, AccessShareLock);
2335 ScanKeyInit(&skey[0],
2336 Anum_pg_opclass_opcmethod,
2337 BTEqualStrategyNumber, F_OIDEQ,
2338 ObjectIdGetDatum(am_id));
2340 scan = systable_beginscan(rel, OpclassAmNameNspIndexId, true,
2341 NULL, 1, skey);
2343 while (HeapTupleIsValid(tup = systable_getnext(scan)))
2345 Form_pg_opclass opclass = (Form_pg_opclass) GETSTRUCT(tup);
2347 /* ignore altogether if not a default opclass */
2348 if (!opclass->opcdefault)
2349 continue;
2350 if (opclass->opcintype == type_id)
2352 nexact++;
2353 result = opclass->oid;
2355 else if (nexact == 0 &&
2356 IsBinaryCoercible(type_id, opclass->opcintype))
2358 if (IsPreferredType(tcategory, opclass->opcintype))
2360 ncompatiblepreferred++;
2361 result = opclass->oid;
2363 else if (ncompatiblepreferred == 0)
2365 ncompatible++;
2366 result = opclass->oid;
2371 systable_endscan(scan);
2373 table_close(rel, AccessShareLock);
2375 /* raise error if pg_opclass contains inconsistent data */
2376 if (nexact > 1)
2377 ereport(ERROR,
2378 (errcode(ERRCODE_DUPLICATE_OBJECT),
2379 errmsg("there are multiple default operator classes for data type %s",
2380 format_type_be(type_id))));
2382 if (nexact == 1 ||
2383 ncompatiblepreferred == 1 ||
2384 (ncompatiblepreferred == 0 && ncompatible == 1))
2385 return result;
2387 return InvalidOid;
2391 * makeObjectName()
2393 * Create a name for an implicitly created index, sequence, constraint,
2394 * extended statistics, etc.
2396 * The parameters are typically: the original table name, the original field
2397 * name, and a "type" string (such as "seq" or "pkey"). The field name
2398 * and/or type can be NULL if not relevant.
2400 * The result is a palloc'd string.
2402 * The basic result we want is "name1_name2_label", omitting "_name2" or
2403 * "_label" when those parameters are NULL. However, we must generate
2404 * a name with less than NAMEDATALEN characters! So, we truncate one or
2405 * both names if necessary to make a short-enough string. The label part
2406 * is never truncated (so it had better be reasonably short).
2408 * The caller is responsible for checking uniqueness of the generated
2409 * name and retrying as needed; retrying will be done by altering the
2410 * "label" string (which is why we never truncate that part).
2412 char *
2413 makeObjectName(const char *name1, const char *name2, const char *label)
2415 char *name;
2416 int overhead = 0; /* chars needed for label and underscores */
2417 int availchars; /* chars available for name(s) */
2418 int name1chars; /* chars allocated to name1 */
2419 int name2chars; /* chars allocated to name2 */
2420 int ndx;
2422 name1chars = strlen(name1);
2423 if (name2)
2425 name2chars = strlen(name2);
2426 overhead++; /* allow for separating underscore */
2428 else
2429 name2chars = 0;
2430 if (label)
2431 overhead += strlen(label) + 1;
2433 availchars = NAMEDATALEN - 1 - overhead;
2434 Assert(availchars > 0); /* else caller chose a bad label */
2437 * If we must truncate, preferentially truncate the longer name. This
2438 * logic could be expressed without a loop, but it's simple and obvious as
2439 * a loop.
2441 while (name1chars + name2chars > availchars)
2443 if (name1chars > name2chars)
2444 name1chars--;
2445 else
2446 name2chars--;
2449 name1chars = pg_mbcliplen(name1, name1chars, name1chars);
2450 if (name2)
2451 name2chars = pg_mbcliplen(name2, name2chars, name2chars);
2453 /* Now construct the string using the chosen lengths */
2454 name = palloc(name1chars + name2chars + overhead + 1);
2455 memcpy(name, name1, name1chars);
2456 ndx = name1chars;
2457 if (name2)
2459 name[ndx++] = '_';
2460 memcpy(name + ndx, name2, name2chars);
2461 ndx += name2chars;
2463 if (label)
2465 name[ndx++] = '_';
2466 strcpy(name + ndx, label);
2468 else
2469 name[ndx] = '\0';
2471 return name;
2475 * Select a nonconflicting name for a new relation. This is ordinarily
2476 * used to choose index names (which is why it's here) but it can also
2477 * be used for sequences, or any autogenerated relation kind.
2479 * name1, name2, and label are used the same way as for makeObjectName(),
2480 * except that the label can't be NULL; digits will be appended to the label
2481 * if needed to create a name that is unique within the specified namespace.
2483 * If isconstraint is true, we also avoid choosing a name matching any
2484 * existing constraint in the same namespace. (This is stricter than what
2485 * Postgres itself requires, but the SQL standard says that constraint names
2486 * should be unique within schemas, so we follow that for autogenerated
2487 * constraint names.)
2489 * Note: it is theoretically possible to get a collision anyway, if someone
2490 * else chooses the same name concurrently. This is fairly unlikely to be
2491 * a problem in practice, especially if one is holding an exclusive lock on
2492 * the relation identified by name1. However, if choosing multiple names
2493 * within a single command, you'd better create the new object and do
2494 * CommandCounterIncrement before choosing the next one!
2496 * Returns a palloc'd string.
2498 char *
2499 ChooseRelationName(const char *name1, const char *name2,
2500 const char *label, Oid namespaceid,
2501 bool isconstraint)
2503 int pass = 0;
2504 char *relname = NULL;
2505 char modlabel[NAMEDATALEN];
2507 /* try the unmodified label first */
2508 strlcpy(modlabel, label, sizeof(modlabel));
2510 for (;;)
2512 relname = makeObjectName(name1, name2, modlabel);
2514 if (!OidIsValid(get_relname_relid(relname, namespaceid)))
2516 if (!isconstraint ||
2517 !ConstraintNameExists(relname, namespaceid))
2518 break;
2521 /* found a conflict, so try a new name component */
2522 pfree(relname);
2523 snprintf(modlabel, sizeof(modlabel), "%s%d", label, ++pass);
2526 return relname;
2530 * Select the name to be used for an index.
2532 * The argument list is pretty ad-hoc :-(
2534 static char *
2535 ChooseIndexName(const char *tabname, Oid namespaceId,
2536 const List *colnames, const List *exclusionOpNames,
2537 bool primary, bool isconstraint)
2539 char *indexname;
2541 if (primary)
2543 /* the primary key's name does not depend on the specific column(s) */
2544 indexname = ChooseRelationName(tabname,
2545 NULL,
2546 "pkey",
2547 namespaceId,
2548 true);
2550 else if (exclusionOpNames != NIL)
2552 indexname = ChooseRelationName(tabname,
2553 ChooseIndexNameAddition(colnames),
2554 "excl",
2555 namespaceId,
2556 true);
2558 else if (isconstraint)
2560 indexname = ChooseRelationName(tabname,
2561 ChooseIndexNameAddition(colnames),
2562 "key",
2563 namespaceId,
2564 true);
2566 else
2568 indexname = ChooseRelationName(tabname,
2569 ChooseIndexNameAddition(colnames),
2570 "idx",
2571 namespaceId,
2572 false);
2575 return indexname;
2579 * Generate "name2" for a new index given the list of column names for it
2580 * (as produced by ChooseIndexColumnNames). This will be passed to
2581 * ChooseRelationName along with the parent table name and a suitable label.
2583 * We know that less than NAMEDATALEN characters will actually be used,
2584 * so we can truncate the result once we've generated that many.
2586 * XXX See also ChooseForeignKeyConstraintNameAddition and
2587 * ChooseExtendedStatisticNameAddition.
2589 static char *
2590 ChooseIndexNameAddition(const List *colnames)
2592 char buf[NAMEDATALEN * 2];
2593 int buflen = 0;
2594 ListCell *lc;
2596 buf[0] = '\0';
2597 foreach(lc, colnames)
2599 const char *name = (const char *) lfirst(lc);
2601 if (buflen > 0)
2602 buf[buflen++] = '_'; /* insert _ between names */
2605 * At this point we have buflen <= NAMEDATALEN. name should be less
2606 * than NAMEDATALEN already, but use strlcpy for paranoia.
2608 strlcpy(buf + buflen, name, NAMEDATALEN);
2609 buflen += strlen(buf + buflen);
2610 if (buflen >= NAMEDATALEN)
2611 break;
2613 return pstrdup(buf);
2617 * Select the actual names to be used for the columns of an index, given the
2618 * list of IndexElems for the columns. This is mostly about ensuring the
2619 * names are unique so we don't get a conflicting-attribute-names error.
2621 * Returns a List of plain strings (char *, not String nodes).
2623 static List *
2624 ChooseIndexColumnNames(const List *indexElems)
2626 List *result = NIL;
2627 ListCell *lc;
2629 foreach(lc, indexElems)
2631 IndexElem *ielem = (IndexElem *) lfirst(lc);
2632 const char *origname;
2633 const char *curname;
2634 int i;
2635 char buf[NAMEDATALEN];
2637 /* Get the preliminary name from the IndexElem */
2638 if (ielem->indexcolname)
2639 origname = ielem->indexcolname; /* caller-specified name */
2640 else if (ielem->name)
2641 origname = ielem->name; /* simple column reference */
2642 else
2643 origname = "expr"; /* default name for expression */
2645 /* If it conflicts with any previous column, tweak it */
2646 curname = origname;
2647 for (i = 1;; i++)
2649 ListCell *lc2;
2650 char nbuf[32];
2651 int nlen;
2653 foreach(lc2, result)
2655 if (strcmp(curname, (char *) lfirst(lc2)) == 0)
2656 break;
2658 if (lc2 == NULL)
2659 break; /* found nonconflicting name */
2661 sprintf(nbuf, "%d", i);
2663 /* Ensure generated names are shorter than NAMEDATALEN */
2664 nlen = pg_mbcliplen(origname, strlen(origname),
2665 NAMEDATALEN - 1 - strlen(nbuf));
2666 memcpy(buf, origname, nlen);
2667 strcpy(buf + nlen, nbuf);
2668 curname = buf;
2671 /* And attach to the result list */
2672 result = lappend(result, pstrdup(curname));
2674 return result;
2678 * ExecReindex
2680 * Primary entry point for manual REINDEX commands. This is mainly a
2681 * preparation wrapper for the real operations that will happen in
2682 * each subroutine of REINDEX.
2684 void
2685 ExecReindex(ParseState *pstate, const ReindexStmt *stmt, bool isTopLevel)
2687 ReindexParams params = {0};
2688 ListCell *lc;
2689 bool concurrently = false;
2690 bool verbose = false;
2691 char *tablespacename = NULL;
2693 /* Parse option list */
2694 foreach(lc, stmt->params)
2696 DefElem *opt = (DefElem *) lfirst(lc);
2698 if (strcmp(opt->defname, "verbose") == 0)
2699 verbose = defGetBoolean(opt);
2700 else if (strcmp(opt->defname, "concurrently") == 0)
2701 concurrently = defGetBoolean(opt);
2702 else if (strcmp(opt->defname, "tablespace") == 0)
2703 tablespacename = defGetString(opt);
2704 else
2705 ereport(ERROR,
2706 (errcode(ERRCODE_SYNTAX_ERROR),
2707 errmsg("unrecognized REINDEX option \"%s\"",
2708 opt->defname),
2709 parser_errposition(pstate, opt->location)));
2712 if (concurrently)
2713 PreventInTransactionBlock(isTopLevel,
2714 "REINDEX CONCURRENTLY");
2716 params.options =
2717 (verbose ? REINDEXOPT_VERBOSE : 0) |
2718 (concurrently ? REINDEXOPT_CONCURRENTLY : 0);
2721 * Assign the tablespace OID to move indexes to, with InvalidOid to do
2722 * nothing.
2724 if (tablespacename != NULL)
2726 params.tablespaceOid = get_tablespace_oid(tablespacename, false);
2728 /* Check permissions except when moving to database's default */
2729 if (OidIsValid(params.tablespaceOid) &&
2730 params.tablespaceOid != MyDatabaseTableSpace)
2732 AclResult aclresult;
2734 aclresult = object_aclcheck(TableSpaceRelationId, params.tablespaceOid,
2735 GetUserId(), ACL_CREATE);
2736 if (aclresult != ACLCHECK_OK)
2737 aclcheck_error(aclresult, OBJECT_TABLESPACE,
2738 get_tablespace_name(params.tablespaceOid));
2741 else
2742 params.tablespaceOid = InvalidOid;
2744 switch (stmt->kind)
2746 case REINDEX_OBJECT_INDEX:
2747 ReindexIndex(stmt, &params, isTopLevel);
2748 break;
2749 case REINDEX_OBJECT_TABLE:
2750 ReindexTable(stmt, &params, isTopLevel);
2751 break;
2752 case REINDEX_OBJECT_SCHEMA:
2753 case REINDEX_OBJECT_SYSTEM:
2754 case REINDEX_OBJECT_DATABASE:
2757 * This cannot run inside a user transaction block; if we were
2758 * inside a transaction, then its commit- and
2759 * start-transaction-command calls would not have the intended
2760 * effect!
2762 PreventInTransactionBlock(isTopLevel,
2763 (stmt->kind == REINDEX_OBJECT_SCHEMA) ? "REINDEX SCHEMA" :
2764 (stmt->kind == REINDEX_OBJECT_SYSTEM) ? "REINDEX SYSTEM" :
2765 "REINDEX DATABASE");
2766 ReindexMultipleTables(stmt, &params);
2767 break;
2768 default:
2769 elog(ERROR, "unrecognized object type: %d",
2770 (int) stmt->kind);
2771 break;
2776 * ReindexIndex
2777 * Recreate a specific index.
2779 static void
2780 ReindexIndex(const ReindexStmt *stmt, const ReindexParams *params, bool isTopLevel)
2782 const RangeVar *indexRelation = stmt->relation;
2783 struct ReindexIndexCallbackState state;
2784 Oid indOid;
2785 char persistence;
2786 char relkind;
2789 * Find and lock index, and check permissions on table; use callback to
2790 * obtain lock on table first, to avoid deadlock hazard. The lock level
2791 * used here must match the index lock obtained in reindex_index().
2793 * If it's a temporary index, we will perform a non-concurrent reindex,
2794 * even if CONCURRENTLY was requested. In that case, reindex_index() will
2795 * upgrade the lock, but that's OK, because other sessions can't hold
2796 * locks on our temporary table.
2798 state.params = *params;
2799 state.locked_table_oid = InvalidOid;
2800 indOid = RangeVarGetRelidExtended(indexRelation,
2801 (params->options & REINDEXOPT_CONCURRENTLY) != 0 ?
2802 ShareUpdateExclusiveLock : AccessExclusiveLock,
2804 RangeVarCallbackForReindexIndex,
2805 &state);
2808 * Obtain the current persistence and kind of the existing index. We
2809 * already hold a lock on the index.
2811 persistence = get_rel_persistence(indOid);
2812 relkind = get_rel_relkind(indOid);
2814 if (relkind == RELKIND_PARTITIONED_INDEX)
2815 ReindexPartitions(stmt, indOid, params, isTopLevel);
2816 else if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
2817 persistence != RELPERSISTENCE_TEMP)
2818 ReindexRelationConcurrently(stmt, indOid, params);
2819 else
2821 ReindexParams newparams = *params;
2823 newparams.options |= REINDEXOPT_REPORT_PROGRESS;
2824 reindex_index(stmt, indOid, false, persistence, &newparams);
2829 * Check permissions on table before acquiring relation lock; also lock
2830 * the heap before the RangeVarGetRelidExtended takes the index lock, to avoid
2831 * deadlocks.
2833 static void
2834 RangeVarCallbackForReindexIndex(const RangeVar *relation,
2835 Oid relId, Oid oldRelId, void *arg)
2837 char relkind;
2838 struct ReindexIndexCallbackState *state = arg;
2839 LOCKMODE table_lockmode;
2840 Oid table_oid;
2843 * Lock level here should match table lock in reindex_index() for
2844 * non-concurrent case and table locks used by index_concurrently_*() for
2845 * concurrent case.
2847 table_lockmode = (state->params.options & REINDEXOPT_CONCURRENTLY) != 0 ?
2848 ShareUpdateExclusiveLock : ShareLock;
2851 * If we previously locked some other index's heap, and the name we're
2852 * looking up no longer refers to that relation, release the now-useless
2853 * lock.
2855 if (relId != oldRelId && OidIsValid(oldRelId))
2857 UnlockRelationOid(state->locked_table_oid, table_lockmode);
2858 state->locked_table_oid = InvalidOid;
2861 /* If the relation does not exist, there's nothing more to do. */
2862 if (!OidIsValid(relId))
2863 return;
2866 * If the relation does exist, check whether it's an index. But note that
2867 * the relation might have been dropped between the time we did the name
2868 * lookup and now. In that case, there's nothing to do.
2870 relkind = get_rel_relkind(relId);
2871 if (!relkind)
2872 return;
2873 if (relkind != RELKIND_INDEX &&
2874 relkind != RELKIND_PARTITIONED_INDEX)
2875 ereport(ERROR,
2876 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2877 errmsg("\"%s\" is not an index", relation->relname)));
2879 /* Check permissions */
2880 table_oid = IndexGetRelation(relId, true);
2881 if (OidIsValid(table_oid))
2883 AclResult aclresult;
2885 aclresult = pg_class_aclcheck(table_oid, GetUserId(), ACL_MAINTAIN);
2886 if (aclresult != ACLCHECK_OK)
2887 aclcheck_error(aclresult, OBJECT_INDEX, relation->relname);
2890 /* Lock heap before index to avoid deadlock. */
2891 if (relId != oldRelId)
2894 * If the OID isn't valid, it means the index was concurrently
2895 * dropped, which is not a problem for us; just return normally.
2897 if (OidIsValid(table_oid))
2899 LockRelationOid(table_oid, table_lockmode);
2900 state->locked_table_oid = table_oid;
2906 * ReindexTable
2907 * Recreate all indexes of a table (and of its toast table, if any)
2909 static Oid
2910 ReindexTable(const ReindexStmt *stmt, const ReindexParams *params, bool isTopLevel)
2912 Oid heapOid;
2913 bool result;
2914 const RangeVar *relation = stmt->relation;
2917 * The lock level used here should match reindex_relation().
2919 * If it's a temporary table, we will perform a non-concurrent reindex,
2920 * even if CONCURRENTLY was requested. In that case, reindex_relation()
2921 * will upgrade the lock, but that's OK, because other sessions can't hold
2922 * locks on our temporary table.
2924 heapOid = RangeVarGetRelidExtended(relation,
2925 (params->options & REINDEXOPT_CONCURRENTLY) != 0 ?
2926 ShareUpdateExclusiveLock : ShareLock,
2928 RangeVarCallbackMaintainsTable, NULL);
2930 if (get_rel_relkind(heapOid) == RELKIND_PARTITIONED_TABLE)
2931 ReindexPartitions(stmt, heapOid, params, isTopLevel);
2932 else if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
2933 get_rel_persistence(heapOid) != RELPERSISTENCE_TEMP)
2935 result = ReindexRelationConcurrently(stmt, heapOid, params);
2937 if (!result)
2938 ereport(NOTICE,
2939 (errmsg("table \"%s\" has no indexes that can be reindexed concurrently",
2940 relation->relname)));
2942 else
2944 ReindexParams newparams = *params;
2946 newparams.options |= REINDEXOPT_REPORT_PROGRESS;
2947 result = reindex_relation(stmt, heapOid,
2948 REINDEX_REL_PROCESS_TOAST |
2949 REINDEX_REL_CHECK_CONSTRAINTS,
2950 &newparams);
2951 if (!result)
2952 ereport(NOTICE,
2953 (errmsg("table \"%s\" has no indexes to reindex",
2954 relation->relname)));
2957 return heapOid;
2961 * ReindexMultipleTables
2962 * Recreate indexes of tables selected by objectName/objectKind.
2964 * To reduce the probability of deadlocks, each table is reindexed in a
2965 * separate transaction, so we can release the lock on it right away.
2966 * That means this must not be called within a user transaction block!
2968 static void
2969 ReindexMultipleTables(const ReindexStmt *stmt, const ReindexParams *params)
2972 Oid objectOid;
2973 Relation relationRelation;
2974 TableScanDesc scan;
2975 ScanKeyData scan_keys[1];
2976 HeapTuple tuple;
2977 MemoryContext private_context;
2978 MemoryContext old;
2979 List *relids = NIL;
2980 int num_keys;
2981 bool concurrent_warning = false;
2982 bool tablespace_warning = false;
2983 const char *objectName = stmt->name;
2984 const ReindexObjectType objectKind = stmt->kind;
2986 Assert(objectKind == REINDEX_OBJECT_SCHEMA ||
2987 objectKind == REINDEX_OBJECT_SYSTEM ||
2988 objectKind == REINDEX_OBJECT_DATABASE);
2991 * This matches the options enforced by the grammar, where the object name
2992 * is optional for DATABASE and SYSTEM.
2994 Assert(objectName || objectKind != REINDEX_OBJECT_SCHEMA);
2996 if (objectKind == REINDEX_OBJECT_SYSTEM &&
2997 (params->options & REINDEXOPT_CONCURRENTLY) != 0)
2998 ereport(ERROR,
2999 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3000 errmsg("cannot reindex system catalogs concurrently")));
3003 * Get OID of object to reindex, being the database currently being used
3004 * by session for a database or for system catalogs, or the schema defined
3005 * by caller. At the same time do permission checks that need different
3006 * processing depending on the object type.
3008 if (objectKind == REINDEX_OBJECT_SCHEMA)
3010 objectOid = get_namespace_oid(objectName, false);
3012 if (!object_ownercheck(NamespaceRelationId, objectOid, GetUserId()) &&
3013 !has_privs_of_role(GetUserId(), ROLE_PG_MAINTAIN))
3014 aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_SCHEMA,
3015 objectName);
3017 else
3019 objectOid = MyDatabaseId;
3021 if (objectName && strcmp(objectName, get_database_name(objectOid)) != 0)
3022 ereport(ERROR,
3023 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3024 errmsg("can only reindex the currently open database")));
3025 if (!object_ownercheck(DatabaseRelationId, objectOid, GetUserId()) &&
3026 !has_privs_of_role(GetUserId(), ROLE_PG_MAINTAIN))
3027 aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_DATABASE,
3028 get_database_name(objectOid));
3032 * Create a memory context that will survive forced transaction commits we
3033 * do below. Since it is a child of PortalContext, it will go away
3034 * eventually even if we suffer an error; there's no need for special
3035 * abort cleanup logic.
3037 private_context = AllocSetContextCreate(PortalContext,
3038 "ReindexMultipleTables",
3039 ALLOCSET_SMALL_SIZES);
3042 * Define the search keys to find the objects to reindex. For a schema, we
3043 * select target relations using relnamespace, something not necessary for
3044 * a database-wide operation.
3046 if (objectKind == REINDEX_OBJECT_SCHEMA)
3048 num_keys = 1;
3049 ScanKeyInit(&scan_keys[0],
3050 Anum_pg_class_relnamespace,
3051 BTEqualStrategyNumber, F_OIDEQ,
3052 ObjectIdGetDatum(objectOid));
3054 else
3055 num_keys = 0;
3058 * Scan pg_class to build a list of the relations we need to reindex.
3060 * We only consider plain relations and materialized views here (toast
3061 * rels will be processed indirectly by reindex_relation).
3063 relationRelation = table_open(RelationRelationId, AccessShareLock);
3064 scan = table_beginscan_catalog(relationRelation, num_keys, scan_keys);
3065 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
3067 Form_pg_class classtuple = (Form_pg_class) GETSTRUCT(tuple);
3068 Oid relid = classtuple->oid;
3071 * Only regular tables and matviews can have indexes, so ignore any
3072 * other kind of relation.
3074 * Partitioned tables/indexes are skipped but matching leaf partitions
3075 * are processed.
3077 if (classtuple->relkind != RELKIND_RELATION &&
3078 classtuple->relkind != RELKIND_MATVIEW)
3079 continue;
3081 /* Skip temp tables of other backends; we can't reindex them at all */
3082 if (classtuple->relpersistence == RELPERSISTENCE_TEMP &&
3083 !isTempNamespace(classtuple->relnamespace))
3084 continue;
3087 * Check user/system classification. SYSTEM processes all the
3088 * catalogs, and DATABASE processes everything that's not a catalog.
3090 if (objectKind == REINDEX_OBJECT_SYSTEM &&
3091 !IsCatalogRelationOid(relid))
3092 continue;
3093 else if (objectKind == REINDEX_OBJECT_DATABASE &&
3094 IsCatalogRelationOid(relid))
3095 continue;
3098 * We already checked privileges on the database or schema, but we
3099 * further restrict reindexing shared catalogs to roles with the
3100 * MAINTAIN privilege on the relation.
3102 if (classtuple->relisshared &&
3103 pg_class_aclcheck(relid, GetUserId(), ACL_MAINTAIN) != ACLCHECK_OK)
3104 continue;
3107 * Skip system tables, since index_create() would reject indexing them
3108 * concurrently (and it would likely fail if we tried).
3110 if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
3111 IsCatalogRelationOid(relid))
3113 if (!concurrent_warning)
3114 ereport(WARNING,
3115 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3116 errmsg("cannot reindex system catalogs concurrently, skipping all")));
3117 concurrent_warning = true;
3118 continue;
3122 * If a new tablespace is set, check if this relation has to be
3123 * skipped.
3125 if (OidIsValid(params->tablespaceOid))
3127 bool skip_rel = false;
3130 * Mapped relations cannot be moved to different tablespaces (in
3131 * particular this eliminates all shared catalogs.).
3133 if (RELKIND_HAS_STORAGE(classtuple->relkind) &&
3134 !RelFileNumberIsValid(classtuple->relfilenode))
3135 skip_rel = true;
3138 * A system relation is always skipped, even with
3139 * allow_system_table_mods enabled.
3141 if (IsSystemClass(relid, classtuple))
3142 skip_rel = true;
3144 if (skip_rel)
3146 if (!tablespace_warning)
3147 ereport(WARNING,
3148 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3149 errmsg("cannot move system relations, skipping all")));
3150 tablespace_warning = true;
3151 continue;
3155 /* Save the list of relation OIDs in private context */
3156 old = MemoryContextSwitchTo(private_context);
3159 * We always want to reindex pg_class first if it's selected to be
3160 * reindexed. This ensures that if there is any corruption in
3161 * pg_class' indexes, they will be fixed before we process any other
3162 * tables. This is critical because reindexing itself will try to
3163 * update pg_class.
3165 if (relid == RelationRelationId)
3166 relids = lcons_oid(relid, relids);
3167 else
3168 relids = lappend_oid(relids, relid);
3170 MemoryContextSwitchTo(old);
3172 table_endscan(scan);
3173 table_close(relationRelation, AccessShareLock);
3176 * Process each relation listed in a separate transaction. Note that this
3177 * commits and then starts a new transaction immediately.
3179 ReindexMultipleInternal(stmt, relids, params);
3181 MemoryContextDelete(private_context);
3185 * Error callback specific to ReindexPartitions().
3187 static void
3188 reindex_error_callback(void *arg)
3190 ReindexErrorInfo *errinfo = (ReindexErrorInfo *) arg;
3192 Assert(RELKIND_HAS_PARTITIONS(errinfo->relkind));
3194 if (errinfo->relkind == RELKIND_PARTITIONED_TABLE)
3195 errcontext("while reindexing partitioned table \"%s.%s\"",
3196 errinfo->relnamespace, errinfo->relname);
3197 else if (errinfo->relkind == RELKIND_PARTITIONED_INDEX)
3198 errcontext("while reindexing partitioned index \"%s.%s\"",
3199 errinfo->relnamespace, errinfo->relname);
3203 * ReindexPartitions
3205 * Reindex a set of partitions, per the partitioned index or table given
3206 * by the caller.
3208 static void
3209 ReindexPartitions(const ReindexStmt *stmt, Oid relid, const ReindexParams *params, bool isTopLevel)
3211 List *partitions = NIL;
3212 char relkind = get_rel_relkind(relid);
3213 char *relname = get_rel_name(relid);
3214 char *relnamespace = get_namespace_name(get_rel_namespace(relid));
3215 MemoryContext reindex_context;
3216 List *inhoids;
3217 ListCell *lc;
3218 ErrorContextCallback errcallback;
3219 ReindexErrorInfo errinfo;
3221 Assert(RELKIND_HAS_PARTITIONS(relkind));
3224 * Check if this runs in a transaction block, with an error callback to
3225 * provide more context under which a problem happens.
3227 errinfo.relname = pstrdup(relname);
3228 errinfo.relnamespace = pstrdup(relnamespace);
3229 errinfo.relkind = relkind;
3230 errcallback.callback = reindex_error_callback;
3231 errcallback.arg = (void *) &errinfo;
3232 errcallback.previous = error_context_stack;
3233 error_context_stack = &errcallback;
3235 PreventInTransactionBlock(isTopLevel,
3236 relkind == RELKIND_PARTITIONED_TABLE ?
3237 "REINDEX TABLE" : "REINDEX INDEX");
3239 /* Pop the error context stack */
3240 error_context_stack = errcallback.previous;
3243 * Create special memory context for cross-transaction storage.
3245 * Since it is a child of PortalContext, it will go away eventually even
3246 * if we suffer an error so there is no need for special abort cleanup
3247 * logic.
3249 reindex_context = AllocSetContextCreate(PortalContext, "Reindex",
3250 ALLOCSET_DEFAULT_SIZES);
3252 /* ShareLock is enough to prevent schema modifications */
3253 inhoids = find_all_inheritors(relid, ShareLock, NULL);
3256 * The list of relations to reindex are the physical partitions of the
3257 * tree so discard any partitioned table or index.
3259 foreach(lc, inhoids)
3261 Oid partoid = lfirst_oid(lc);
3262 char partkind = get_rel_relkind(partoid);
3263 MemoryContext old_context;
3266 * This discards partitioned tables, partitioned indexes and foreign
3267 * tables.
3269 if (!RELKIND_HAS_STORAGE(partkind))
3270 continue;
3272 Assert(partkind == RELKIND_INDEX ||
3273 partkind == RELKIND_RELATION);
3275 /* Save partition OID */
3276 old_context = MemoryContextSwitchTo(reindex_context);
3277 partitions = lappend_oid(partitions, partoid);
3278 MemoryContextSwitchTo(old_context);
3282 * Process each partition listed in a separate transaction. Note that
3283 * this commits and then starts a new transaction immediately.
3285 ReindexMultipleInternal(stmt, partitions, params);
3288 * Clean up working storage --- note we must do this after
3289 * StartTransactionCommand, else we might be trying to delete the active
3290 * context!
3292 MemoryContextDelete(reindex_context);
3296 * ReindexMultipleInternal
3298 * Reindex a list of relations, each one being processed in its own
3299 * transaction. This commits the existing transaction immediately,
3300 * and starts a new transaction when finished.
3302 static void
3303 ReindexMultipleInternal(const ReindexStmt *stmt, const List *relids, const ReindexParams *params)
3305 ListCell *l;
3307 PopActiveSnapshot();
3308 CommitTransactionCommand();
3310 foreach(l, relids)
3312 Oid relid = lfirst_oid(l);
3313 char relkind;
3314 char relpersistence;
3316 StartTransactionCommand();
3318 /* functions in indexes may want a snapshot set */
3319 PushActiveSnapshot(GetTransactionSnapshot());
3321 /* check if the relation still exists */
3322 if (!SearchSysCacheExists1(RELOID, ObjectIdGetDatum(relid)))
3324 PopActiveSnapshot();
3325 CommitTransactionCommand();
3326 continue;
3330 * Check permissions except when moving to database's default if a new
3331 * tablespace is chosen. Note that this check also happens in
3332 * ExecReindex(), but we do an extra check here as this runs across
3333 * multiple transactions.
3335 if (OidIsValid(params->tablespaceOid) &&
3336 params->tablespaceOid != MyDatabaseTableSpace)
3338 AclResult aclresult;
3340 aclresult = object_aclcheck(TableSpaceRelationId, params->tablespaceOid,
3341 GetUserId(), ACL_CREATE);
3342 if (aclresult != ACLCHECK_OK)
3343 aclcheck_error(aclresult, OBJECT_TABLESPACE,
3344 get_tablespace_name(params->tablespaceOid));
3347 relkind = get_rel_relkind(relid);
3348 relpersistence = get_rel_persistence(relid);
3351 * Partitioned tables and indexes can never be processed directly, and
3352 * a list of their leaves should be built first.
3354 Assert(!RELKIND_HAS_PARTITIONS(relkind));
3356 if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
3357 relpersistence != RELPERSISTENCE_TEMP)
3359 ReindexParams newparams = *params;
3361 newparams.options |= REINDEXOPT_MISSING_OK;
3362 (void) ReindexRelationConcurrently(stmt, relid, &newparams);
3363 if (ActiveSnapshotSet())
3364 PopActiveSnapshot();
3365 /* ReindexRelationConcurrently() does the verbose output */
3367 else if (relkind == RELKIND_INDEX)
3369 ReindexParams newparams = *params;
3371 newparams.options |=
3372 REINDEXOPT_REPORT_PROGRESS | REINDEXOPT_MISSING_OK;
3373 reindex_index(stmt, relid, false, relpersistence, &newparams);
3374 PopActiveSnapshot();
3375 /* reindex_index() does the verbose output */
3377 else
3379 bool result;
3380 ReindexParams newparams = *params;
3382 newparams.options |=
3383 REINDEXOPT_REPORT_PROGRESS | REINDEXOPT_MISSING_OK;
3384 result = reindex_relation(stmt, relid,
3385 REINDEX_REL_PROCESS_TOAST |
3386 REINDEX_REL_CHECK_CONSTRAINTS,
3387 &newparams);
3389 if (result && (params->options & REINDEXOPT_VERBOSE) != 0)
3390 ereport(INFO,
3391 (errmsg("table \"%s.%s\" was reindexed",
3392 get_namespace_name(get_rel_namespace(relid)),
3393 get_rel_name(relid))));
3395 PopActiveSnapshot();
3398 CommitTransactionCommand();
3401 StartTransactionCommand();
3406 * ReindexRelationConcurrently - process REINDEX CONCURRENTLY for given
3407 * relation OID
3409 * 'relationOid' can either belong to an index, a table or a materialized
3410 * view. For tables and materialized views, all its indexes will be rebuilt,
3411 * excluding invalid indexes and any indexes used in exclusion constraints,
3412 * but including its associated toast table indexes. For indexes, the index
3413 * itself will be rebuilt.
3415 * The locks taken on parent tables and involved indexes are kept until the
3416 * transaction is committed, at which point a session lock is taken on each
3417 * relation. Both of these protect against concurrent schema changes.
3419 * Returns true if any indexes have been rebuilt (including toast table's
3420 * indexes, when relevant), otherwise returns false.
3422 * NOTE: This cannot be used on temporary relations. A concurrent build would
3423 * cause issues with ON COMMIT actions triggered by the transactions of the
3424 * concurrent build. Temporary relations are not subject to concurrent
3425 * concerns, so there's no need for the more complicated concurrent build,
3426 * anyway, and a non-concurrent reindex is more efficient.
3428 static bool
3429 ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const ReindexParams *params)
3431 typedef struct ReindexIndexInfo
3433 Oid indexId;
3434 Oid tableId;
3435 Oid amId;
3436 bool safe; /* for set_indexsafe_procflags */
3437 } ReindexIndexInfo;
3438 List *heapRelationIds = NIL;
3439 List *indexIds = NIL;
3440 List *newIndexIds = NIL;
3441 List *relationLocks = NIL;
3442 List *lockTags = NIL;
3443 ListCell *lc,
3444 *lc2;
3445 MemoryContext private_context;
3446 MemoryContext oldcontext;
3447 char relkind;
3448 char *relationName = NULL;
3449 char *relationNamespace = NULL;
3450 PGRUsage ru0;
3451 const int progress_index[] = {
3452 PROGRESS_CREATEIDX_COMMAND,
3453 PROGRESS_CREATEIDX_PHASE,
3454 PROGRESS_CREATEIDX_INDEX_OID,
3455 PROGRESS_CREATEIDX_ACCESS_METHOD_OID
3457 int64 progress_vals[4];
3460 * Create a memory context that will survive forced transaction commits we
3461 * do below. Since it is a child of PortalContext, it will go away
3462 * eventually even if we suffer an error; there's no need for special
3463 * abort cleanup logic.
3465 private_context = AllocSetContextCreate(PortalContext,
3466 "ReindexConcurrent",
3467 ALLOCSET_SMALL_SIZES);
3469 if ((params->options & REINDEXOPT_VERBOSE) != 0)
3471 /* Save data needed by REINDEX VERBOSE in private context */
3472 oldcontext = MemoryContextSwitchTo(private_context);
3474 relationName = get_rel_name(relationOid);
3475 relationNamespace = get_namespace_name(get_rel_namespace(relationOid));
3477 pg_rusage_init(&ru0);
3479 MemoryContextSwitchTo(oldcontext);
3482 relkind = get_rel_relkind(relationOid);
3485 * Extract the list of indexes that are going to be rebuilt based on the
3486 * relation Oid given by caller.
3488 switch (relkind)
3490 case RELKIND_RELATION:
3491 case RELKIND_MATVIEW:
3492 case RELKIND_TOASTVALUE:
3495 * In the case of a relation, find all its indexes including
3496 * toast indexes.
3498 Relation heapRelation;
3500 /* Save the list of relation OIDs in private context */
3501 oldcontext = MemoryContextSwitchTo(private_context);
3503 /* Track this relation for session locks */
3504 heapRelationIds = lappend_oid(heapRelationIds, relationOid);
3506 MemoryContextSwitchTo(oldcontext);
3508 if (IsCatalogRelationOid(relationOid))
3509 ereport(ERROR,
3510 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3511 errmsg("cannot reindex system catalogs concurrently")));
3513 /* Open relation to get its indexes */
3514 if ((params->options & REINDEXOPT_MISSING_OK) != 0)
3516 heapRelation = try_table_open(relationOid,
3517 ShareUpdateExclusiveLock);
3518 /* leave if relation does not exist */
3519 if (!heapRelation)
3520 break;
3522 else
3523 heapRelation = table_open(relationOid,
3524 ShareUpdateExclusiveLock);
3526 if (OidIsValid(params->tablespaceOid) &&
3527 IsSystemRelation(heapRelation))
3528 ereport(ERROR,
3529 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3530 errmsg("cannot move system relation \"%s\"",
3531 RelationGetRelationName(heapRelation))));
3533 /* Add all the valid indexes of relation to list */
3534 foreach(lc, RelationGetIndexList(heapRelation))
3536 Oid cellOid = lfirst_oid(lc);
3537 Relation indexRelation = index_open(cellOid,
3538 ShareUpdateExclusiveLock);
3540 if (!indexRelation->rd_index->indisvalid)
3541 ereport(WARNING,
3542 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3543 errmsg("skipping reindex of invalid index \"%s.%s\"",
3544 get_namespace_name(get_rel_namespace(cellOid)),
3545 get_rel_name(cellOid)),
3546 errhint("Use DROP INDEX or REINDEX INDEX.")));
3547 else if (indexRelation->rd_index->indisexclusion)
3548 ereport(WARNING,
3549 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3550 errmsg("cannot reindex exclusion constraint index \"%s.%s\" concurrently, skipping",
3551 get_namespace_name(get_rel_namespace(cellOid)),
3552 get_rel_name(cellOid))));
3553 else
3555 ReindexIndexInfo *idx;
3557 /* Save the list of relation OIDs in private context */
3558 oldcontext = MemoryContextSwitchTo(private_context);
3560 idx = palloc_object(ReindexIndexInfo);
3561 idx->indexId = cellOid;
3562 /* other fields set later */
3564 indexIds = lappend(indexIds, idx);
3566 MemoryContextSwitchTo(oldcontext);
3569 index_close(indexRelation, NoLock);
3572 /* Also add the toast indexes */
3573 if (OidIsValid(heapRelation->rd_rel->reltoastrelid))
3575 Oid toastOid = heapRelation->rd_rel->reltoastrelid;
3576 Relation toastRelation = table_open(toastOid,
3577 ShareUpdateExclusiveLock);
3579 /* Save the list of relation OIDs in private context */
3580 oldcontext = MemoryContextSwitchTo(private_context);
3582 /* Track this relation for session locks */
3583 heapRelationIds = lappend_oid(heapRelationIds, toastOid);
3585 MemoryContextSwitchTo(oldcontext);
3587 foreach(lc2, RelationGetIndexList(toastRelation))
3589 Oid cellOid = lfirst_oid(lc2);
3590 Relation indexRelation = index_open(cellOid,
3591 ShareUpdateExclusiveLock);
3593 if (!indexRelation->rd_index->indisvalid)
3594 ereport(WARNING,
3595 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3596 errmsg("skipping reindex of invalid index \"%s.%s\"",
3597 get_namespace_name(get_rel_namespace(cellOid)),
3598 get_rel_name(cellOid)),
3599 errhint("Use DROP INDEX or REINDEX INDEX.")));
3600 else
3602 ReindexIndexInfo *idx;
3605 * Save the list of relation OIDs in private
3606 * context
3608 oldcontext = MemoryContextSwitchTo(private_context);
3610 idx = palloc_object(ReindexIndexInfo);
3611 idx->indexId = cellOid;
3612 indexIds = lappend(indexIds, idx);
3613 /* other fields set later */
3615 MemoryContextSwitchTo(oldcontext);
3618 index_close(indexRelation, NoLock);
3621 table_close(toastRelation, NoLock);
3624 table_close(heapRelation, NoLock);
3625 break;
3627 case RELKIND_INDEX:
3629 Oid heapId = IndexGetRelation(relationOid,
3630 (params->options & REINDEXOPT_MISSING_OK) != 0);
3631 Relation heapRelation;
3632 ReindexIndexInfo *idx;
3634 /* if relation is missing, leave */
3635 if (!OidIsValid(heapId))
3636 break;
3638 if (IsCatalogRelationOid(heapId))
3639 ereport(ERROR,
3640 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3641 errmsg("cannot reindex system catalogs concurrently")));
3644 * Don't allow reindex for an invalid index on TOAST table, as
3645 * if rebuilt it would not be possible to drop it. Match
3646 * error message in reindex_index().
3648 if (IsToastNamespace(get_rel_namespace(relationOid)) &&
3649 !get_index_isvalid(relationOid))
3650 ereport(ERROR,
3651 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3652 errmsg("cannot reindex invalid index on TOAST table")));
3655 * Check if parent relation can be locked and if it exists,
3656 * this needs to be done at this stage as the list of indexes
3657 * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
3658 * should not be used once all the session locks are taken.
3660 if ((params->options & REINDEXOPT_MISSING_OK) != 0)
3662 heapRelation = try_table_open(heapId,
3663 ShareUpdateExclusiveLock);
3664 /* leave if relation does not exist */
3665 if (!heapRelation)
3666 break;
3668 else
3669 heapRelation = table_open(heapId,
3670 ShareUpdateExclusiveLock);
3672 if (OidIsValid(params->tablespaceOid) &&
3673 IsSystemRelation(heapRelation))
3674 ereport(ERROR,
3675 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3676 errmsg("cannot move system relation \"%s\"",
3677 get_rel_name(relationOid))));
3679 table_close(heapRelation, NoLock);
3681 /* Save the list of relation OIDs in private context */
3682 oldcontext = MemoryContextSwitchTo(private_context);
3684 /* Track the heap relation of this index for session locks */
3685 heapRelationIds = list_make1_oid(heapId);
3688 * Save the list of relation OIDs in private context. Note
3689 * that invalid indexes are allowed here.
3691 idx = palloc_object(ReindexIndexInfo);
3692 idx->indexId = relationOid;
3693 indexIds = lappend(indexIds, idx);
3694 /* other fields set later */
3696 MemoryContextSwitchTo(oldcontext);
3697 break;
3700 case RELKIND_PARTITIONED_TABLE:
3701 case RELKIND_PARTITIONED_INDEX:
3702 default:
3703 /* Return error if type of relation is not supported */
3704 ereport(ERROR,
3705 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3706 errmsg("cannot reindex this type of relation concurrently")));
3707 break;
3711 * Definitely no indexes, so leave. Any checks based on
3712 * REINDEXOPT_MISSING_OK should be done only while the list of indexes to
3713 * work on is built as the session locks taken before this transaction
3714 * commits will make sure that they cannot be dropped by a concurrent
3715 * session until this operation completes.
3717 if (indexIds == NIL)
3718 return false;
3720 /* It's not a shared catalog, so refuse to move it to shared tablespace */
3721 if (params->tablespaceOid == GLOBALTABLESPACE_OID)
3722 ereport(ERROR,
3723 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3724 errmsg("cannot move non-shared relation to tablespace \"%s\"",
3725 get_tablespace_name(params->tablespaceOid))));
3727 Assert(heapRelationIds != NIL);
3729 /*-----
3730 * Now we have all the indexes we want to process in indexIds.
3732 * The phases now are:
3734 * 1. create new indexes in the catalog
3735 * 2. build new indexes
3736 * 3. let new indexes catch up with tuples inserted in the meantime
3737 * 4. swap index names
3738 * 5. mark old indexes as dead
3739 * 6. drop old indexes
3741 * We process each phase for all indexes before moving to the next phase,
3742 * for efficiency.
3746 * Phase 1 of REINDEX CONCURRENTLY
3748 * Create a new index with the same properties as the old one, but it is
3749 * only registered in catalogs and will be built later. Then get session
3750 * locks on all involved tables. See analogous code in DefineIndex() for
3751 * more detailed comments.
3754 foreach(lc, indexIds)
3756 char *concurrentName;
3757 ReindexIndexInfo *idx = lfirst(lc);
3758 ReindexIndexInfo *newidx;
3759 Oid newIndexId;
3760 Relation indexRel;
3761 Relation heapRel;
3762 Oid save_userid;
3763 int save_sec_context;
3764 int save_nestlevel;
3765 Relation newIndexRel;
3766 LockRelId *lockrelid;
3767 Oid tablespaceid;
3769 indexRel = index_open(idx->indexId, ShareUpdateExclusiveLock);
3770 heapRel = table_open(indexRel->rd_index->indrelid,
3771 ShareUpdateExclusiveLock);
3774 * Switch to the table owner's userid, so that any index functions are
3775 * run as that user. Also lock down security-restricted operations
3776 * and arrange to make GUC variable changes local to this command.
3778 GetUserIdAndSecContext(&save_userid, &save_sec_context);
3779 SetUserIdAndSecContext(heapRel->rd_rel->relowner,
3780 save_sec_context | SECURITY_RESTRICTED_OPERATION);
3781 save_nestlevel = NewGUCNestLevel();
3782 RestrictSearchPath();
3784 /* determine safety of this index for set_indexsafe_procflags */
3785 idx->safe = (indexRel->rd_indexprs == NIL &&
3786 indexRel->rd_indpred == NIL);
3787 idx->tableId = RelationGetRelid(heapRel);
3788 idx->amId = indexRel->rd_rel->relam;
3790 /* This function shouldn't be called for temporary relations. */
3791 if (indexRel->rd_rel->relpersistence == RELPERSISTENCE_TEMP)
3792 elog(ERROR, "cannot reindex a temporary table concurrently");
3794 pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, idx->tableId);
3796 progress_vals[0] = PROGRESS_CREATEIDX_COMMAND_REINDEX_CONCURRENTLY;
3797 progress_vals[1] = 0; /* initializing */
3798 progress_vals[2] = idx->indexId;
3799 progress_vals[3] = idx->amId;
3800 pgstat_progress_update_multi_param(4, progress_index, progress_vals);
3802 /* Choose a temporary relation name for the new index */
3803 concurrentName = ChooseRelationName(get_rel_name(idx->indexId),
3804 NULL,
3805 "ccnew",
3806 get_rel_namespace(indexRel->rd_index->indrelid),
3807 false);
3809 /* Choose the new tablespace, indexes of toast tables are not moved */
3810 if (OidIsValid(params->tablespaceOid) &&
3811 heapRel->rd_rel->relkind != RELKIND_TOASTVALUE)
3812 tablespaceid = params->tablespaceOid;
3813 else
3814 tablespaceid = indexRel->rd_rel->reltablespace;
3816 /* Create new index definition based on given index */
3817 newIndexId = index_concurrently_create_copy(heapRel,
3818 idx->indexId,
3819 tablespaceid,
3820 concurrentName);
3823 * Now open the relation of the new index, a session-level lock is
3824 * also needed on it.
3826 newIndexRel = index_open(newIndexId, ShareUpdateExclusiveLock);
3829 * Save the list of OIDs and locks in private context
3831 oldcontext = MemoryContextSwitchTo(private_context);
3833 newidx = palloc_object(ReindexIndexInfo);
3834 newidx->indexId = newIndexId;
3835 newidx->safe = idx->safe;
3836 newidx->tableId = idx->tableId;
3837 newidx->amId = idx->amId;
3839 newIndexIds = lappend(newIndexIds, newidx);
3842 * Save lockrelid to protect each relation from drop then close
3843 * relations. The lockrelid on parent relation is not taken here to
3844 * avoid multiple locks taken on the same relation, instead we rely on
3845 * parentRelationIds built earlier.
3847 lockrelid = palloc_object(LockRelId);
3848 *lockrelid = indexRel->rd_lockInfo.lockRelId;
3849 relationLocks = lappend(relationLocks, lockrelid);
3850 lockrelid = palloc_object(LockRelId);
3851 *lockrelid = newIndexRel->rd_lockInfo.lockRelId;
3852 relationLocks = lappend(relationLocks, lockrelid);
3854 MemoryContextSwitchTo(oldcontext);
3856 index_close(indexRel, NoLock);
3857 index_close(newIndexRel, NoLock);
3859 /* Roll back any GUC changes executed by index functions */
3860 AtEOXact_GUC(false, save_nestlevel);
3862 /* Restore userid and security context */
3863 SetUserIdAndSecContext(save_userid, save_sec_context);
3865 table_close(heapRel, NoLock);
3868 * If a statement is available, telling that this comes from a REINDEX
3869 * command, collect the new index for event triggers.
3871 if (stmt)
3873 ObjectAddress address;
3875 ObjectAddressSet(address, RelationRelationId, newIndexId);
3876 EventTriggerCollectSimpleCommand(address,
3877 InvalidObjectAddress,
3878 (Node *) stmt);
3883 * Save the heap lock for following visibility checks with other backends
3884 * might conflict with this session.
3886 foreach(lc, heapRelationIds)
3888 Relation heapRelation = table_open(lfirst_oid(lc), ShareUpdateExclusiveLock);
3889 LockRelId *lockrelid;
3890 LOCKTAG *heaplocktag;
3892 /* Save the list of locks in private context */
3893 oldcontext = MemoryContextSwitchTo(private_context);
3895 /* Add lockrelid of heap relation to the list of locked relations */
3896 lockrelid = palloc_object(LockRelId);
3897 *lockrelid = heapRelation->rd_lockInfo.lockRelId;
3898 relationLocks = lappend(relationLocks, lockrelid);
3900 heaplocktag = palloc_object(LOCKTAG);
3902 /* Save the LOCKTAG for this parent relation for the wait phase */
3903 SET_LOCKTAG_RELATION(*heaplocktag, lockrelid->dbId, lockrelid->relId);
3904 lockTags = lappend(lockTags, heaplocktag);
3906 MemoryContextSwitchTo(oldcontext);
3908 /* Close heap relation */
3909 table_close(heapRelation, NoLock);
3912 /* Get a session-level lock on each table. */
3913 foreach(lc, relationLocks)
3915 LockRelId *lockrelid = (LockRelId *) lfirst(lc);
3917 LockRelationIdForSession(lockrelid, ShareUpdateExclusiveLock);
3920 PopActiveSnapshot();
3921 CommitTransactionCommand();
3922 StartTransactionCommand();
3925 * Because we don't take a snapshot in this transaction, there's no need
3926 * to set the PROC_IN_SAFE_IC flag here.
3930 * Phase 2 of REINDEX CONCURRENTLY
3932 * Build the new indexes in a separate transaction for each index to avoid
3933 * having open transactions for an unnecessary long time. But before
3934 * doing that, wait until no running transactions could have the table of
3935 * the index open with the old list of indexes. See "phase 2" in
3936 * DefineIndex() for more details.
3939 pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE,
3940 PROGRESS_CREATEIDX_PHASE_WAIT_1);
3941 WaitForLockersMultiple(lockTags, ShareLock, true);
3942 CommitTransactionCommand();
3944 foreach(lc, newIndexIds)
3946 ReindexIndexInfo *newidx = lfirst(lc);
3948 /* Start new transaction for this index's concurrent build */
3949 StartTransactionCommand();
3952 * Check for user-requested abort. This is inside a transaction so as
3953 * xact.c does not issue a useless WARNING, and ensures that
3954 * session-level locks are cleaned up on abort.
3956 CHECK_FOR_INTERRUPTS();
3958 /* Tell concurrent indexing to ignore us, if index qualifies */
3959 if (newidx->safe)
3960 set_indexsafe_procflags();
3962 /* Set ActiveSnapshot since functions in the indexes may need it */
3963 PushActiveSnapshot(GetTransactionSnapshot());
3966 * Update progress for the index to build, with the correct parent
3967 * table involved.
3969 pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, newidx->tableId);
3970 progress_vals[0] = PROGRESS_CREATEIDX_COMMAND_REINDEX_CONCURRENTLY;
3971 progress_vals[1] = PROGRESS_CREATEIDX_PHASE_BUILD;
3972 progress_vals[2] = newidx->indexId;
3973 progress_vals[3] = newidx->amId;
3974 pgstat_progress_update_multi_param(4, progress_index, progress_vals);
3976 /* Perform concurrent build of new index */
3977 index_concurrently_build(newidx->tableId, newidx->indexId);
3979 PopActiveSnapshot();
3980 CommitTransactionCommand();
3983 StartTransactionCommand();
3986 * Because we don't take a snapshot or Xid in this transaction, there's no
3987 * need to set the PROC_IN_SAFE_IC flag here.
3991 * Phase 3 of REINDEX CONCURRENTLY
3993 * During this phase the old indexes catch up with any new tuples that
3994 * were created during the previous phase. See "phase 3" in DefineIndex()
3995 * for more details.
3998 pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE,
3999 PROGRESS_CREATEIDX_PHASE_WAIT_2);
4000 WaitForLockersMultiple(lockTags, ShareLock, true);
4001 CommitTransactionCommand();
4003 foreach(lc, newIndexIds)
4005 ReindexIndexInfo *newidx = lfirst(lc);
4006 TransactionId limitXmin;
4007 Snapshot snapshot;
4009 StartTransactionCommand();
4012 * Check for user-requested abort. This is inside a transaction so as
4013 * xact.c does not issue a useless WARNING, and ensures that
4014 * session-level locks are cleaned up on abort.
4016 CHECK_FOR_INTERRUPTS();
4018 /* Tell concurrent indexing to ignore us, if index qualifies */
4019 if (newidx->safe)
4020 set_indexsafe_procflags();
4023 * Take the "reference snapshot" that will be used by validate_index()
4024 * to filter candidate tuples.
4026 snapshot = RegisterSnapshot(GetTransactionSnapshot());
4027 PushActiveSnapshot(snapshot);
4030 * Update progress for the index to build, with the correct parent
4031 * table involved.
4033 pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, newidx->tableId);
4034 progress_vals[0] = PROGRESS_CREATEIDX_COMMAND_REINDEX_CONCURRENTLY;
4035 progress_vals[1] = PROGRESS_CREATEIDX_PHASE_VALIDATE_IDXSCAN;
4036 progress_vals[2] = newidx->indexId;
4037 progress_vals[3] = newidx->amId;
4038 pgstat_progress_update_multi_param(4, progress_index, progress_vals);
4040 validate_index(newidx->tableId, newidx->indexId, snapshot);
4043 * We can now do away with our active snapshot, we still need to save
4044 * the xmin limit to wait for older snapshots.
4046 limitXmin = snapshot->xmin;
4048 PopActiveSnapshot();
4049 UnregisterSnapshot(snapshot);
4052 * To ensure no deadlocks, we must commit and start yet another
4053 * transaction, and do our wait before any snapshot has been taken in
4054 * it.
4056 CommitTransactionCommand();
4057 StartTransactionCommand();
4060 * The index is now valid in the sense that it contains all currently
4061 * interesting tuples. But since it might not contain tuples deleted
4062 * just before the reference snap was taken, we have to wait out any
4063 * transactions that might have older snapshots.
4065 * Because we don't take a snapshot or Xid in this transaction,
4066 * there's no need to set the PROC_IN_SAFE_IC flag here.
4068 pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE,
4069 PROGRESS_CREATEIDX_PHASE_WAIT_3);
4070 WaitForOlderSnapshots(limitXmin, true);
4072 CommitTransactionCommand();
4076 * Phase 4 of REINDEX CONCURRENTLY
4078 * Now that the new indexes have been validated, swap each new index with
4079 * its corresponding old index.
4081 * We mark the new indexes as valid and the old indexes as not valid at
4082 * the same time to make sure we only get constraint violations from the
4083 * indexes with the correct names.
4086 StartTransactionCommand();
4089 * Because this transaction only does catalog manipulations and doesn't do
4090 * any index operations, we can set the PROC_IN_SAFE_IC flag here
4091 * unconditionally.
4093 set_indexsafe_procflags();
4095 forboth(lc, indexIds, lc2, newIndexIds)
4097 ReindexIndexInfo *oldidx = lfirst(lc);
4098 ReindexIndexInfo *newidx = lfirst(lc2);
4099 char *oldName;
4102 * Check for user-requested abort. This is inside a transaction so as
4103 * xact.c does not issue a useless WARNING, and ensures that
4104 * session-level locks are cleaned up on abort.
4106 CHECK_FOR_INTERRUPTS();
4108 /* Choose a relation name for old index */
4109 oldName = ChooseRelationName(get_rel_name(oldidx->indexId),
4110 NULL,
4111 "ccold",
4112 get_rel_namespace(oldidx->tableId),
4113 false);
4116 * Swap old index with the new one. This also marks the new one as
4117 * valid and the old one as not valid.
4119 index_concurrently_swap(newidx->indexId, oldidx->indexId, oldName);
4122 * Invalidate the relcache for the table, so that after this commit
4123 * all sessions will refresh any cached plans that might reference the
4124 * index.
4126 CacheInvalidateRelcacheByRelid(oldidx->tableId);
4129 * CCI here so that subsequent iterations see the oldName in the
4130 * catalog and can choose a nonconflicting name for their oldName.
4131 * Otherwise, this could lead to conflicts if a table has two indexes
4132 * whose names are equal for the first NAMEDATALEN-minus-a-few
4133 * characters.
4135 CommandCounterIncrement();
4138 /* Commit this transaction and make index swaps visible */
4139 CommitTransactionCommand();
4140 StartTransactionCommand();
4143 * While we could set PROC_IN_SAFE_IC if all indexes qualified, there's no
4144 * real need for that, because we only acquire an Xid after the wait is
4145 * done, and that lasts for a very short period.
4149 * Phase 5 of REINDEX CONCURRENTLY
4151 * Mark the old indexes as dead. First we must wait until no running
4152 * transaction could be using the index for a query. See also
4153 * index_drop() for more details.
4156 pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE,
4157 PROGRESS_CREATEIDX_PHASE_WAIT_4);
4158 WaitForLockersMultiple(lockTags, AccessExclusiveLock, true);
4160 foreach(lc, indexIds)
4162 ReindexIndexInfo *oldidx = lfirst(lc);
4165 * Check for user-requested abort. This is inside a transaction so as
4166 * xact.c does not issue a useless WARNING, and ensures that
4167 * session-level locks are cleaned up on abort.
4169 CHECK_FOR_INTERRUPTS();
4171 index_concurrently_set_dead(oldidx->tableId, oldidx->indexId);
4174 /* Commit this transaction to make the updates visible. */
4175 CommitTransactionCommand();
4176 StartTransactionCommand();
4179 * While we could set PROC_IN_SAFE_IC if all indexes qualified, there's no
4180 * real need for that, because we only acquire an Xid after the wait is
4181 * done, and that lasts for a very short period.
4185 * Phase 6 of REINDEX CONCURRENTLY
4187 * Drop the old indexes.
4190 pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE,
4191 PROGRESS_CREATEIDX_PHASE_WAIT_5);
4192 WaitForLockersMultiple(lockTags, AccessExclusiveLock, true);
4194 PushActiveSnapshot(GetTransactionSnapshot());
4197 ObjectAddresses *objects = new_object_addresses();
4199 foreach(lc, indexIds)
4201 ReindexIndexInfo *idx = lfirst(lc);
4202 ObjectAddress object;
4204 object.classId = RelationRelationId;
4205 object.objectId = idx->indexId;
4206 object.objectSubId = 0;
4208 add_exact_object_address(&object, objects);
4212 * Use PERFORM_DELETION_CONCURRENT_LOCK so that index_drop() uses the
4213 * right lock level.
4215 performMultipleDeletions(objects, DROP_RESTRICT,
4216 PERFORM_DELETION_CONCURRENT_LOCK | PERFORM_DELETION_INTERNAL);
4219 PopActiveSnapshot();
4220 CommitTransactionCommand();
4223 * Finally, release the session-level lock on the table.
4225 foreach(lc, relationLocks)
4227 LockRelId *lockrelid = (LockRelId *) lfirst(lc);
4229 UnlockRelationIdForSession(lockrelid, ShareUpdateExclusiveLock);
4232 /* Start a new transaction to finish process properly */
4233 StartTransactionCommand();
4235 /* Log what we did */
4236 if ((params->options & REINDEXOPT_VERBOSE) != 0)
4238 if (relkind == RELKIND_INDEX)
4239 ereport(INFO,
4240 (errmsg("index \"%s.%s\" was reindexed",
4241 relationNamespace, relationName),
4242 errdetail("%s.",
4243 pg_rusage_show(&ru0))));
4244 else
4246 foreach(lc, newIndexIds)
4248 ReindexIndexInfo *idx = lfirst(lc);
4249 Oid indOid = idx->indexId;
4251 ereport(INFO,
4252 (errmsg("index \"%s.%s\" was reindexed",
4253 get_namespace_name(get_rel_namespace(indOid)),
4254 get_rel_name(indOid))));
4255 /* Don't show rusage here, since it's not per index. */
4258 ereport(INFO,
4259 (errmsg("table \"%s.%s\" was reindexed",
4260 relationNamespace, relationName),
4261 errdetail("%s.",
4262 pg_rusage_show(&ru0))));
4266 MemoryContextDelete(private_context);
4268 pgstat_progress_end_command();
4270 return true;
4274 * Insert or delete an appropriate pg_inherits tuple to make the given index
4275 * be a partition of the indicated parent index.
4277 * This also corrects the pg_depend information for the affected index.
4279 void
4280 IndexSetParentIndex(Relation partitionIdx, Oid parentOid)
4282 Relation pg_inherits;
4283 ScanKeyData key[2];
4284 SysScanDesc scan;
4285 Oid partRelid = RelationGetRelid(partitionIdx);
4286 HeapTuple tuple;
4287 bool fix_dependencies;
4289 /* Make sure this is an index */
4290 Assert(partitionIdx->rd_rel->relkind == RELKIND_INDEX ||
4291 partitionIdx->rd_rel->relkind == RELKIND_PARTITIONED_INDEX);
4294 * Scan pg_inherits for rows linking our index to some parent.
4296 pg_inherits = relation_open(InheritsRelationId, RowExclusiveLock);
4297 ScanKeyInit(&key[0],
4298 Anum_pg_inherits_inhrelid,
4299 BTEqualStrategyNumber, F_OIDEQ,
4300 ObjectIdGetDatum(partRelid));
4301 ScanKeyInit(&key[1],
4302 Anum_pg_inherits_inhseqno,
4303 BTEqualStrategyNumber, F_INT4EQ,
4304 Int32GetDatum(1));
4305 scan = systable_beginscan(pg_inherits, InheritsRelidSeqnoIndexId, true,
4306 NULL, 2, key);
4307 tuple = systable_getnext(scan);
4309 if (!HeapTupleIsValid(tuple))
4311 if (parentOid == InvalidOid)
4314 * No pg_inherits row, and no parent wanted: nothing to do in this
4315 * case.
4317 fix_dependencies = false;
4319 else
4321 StoreSingleInheritance(partRelid, parentOid, 1);
4322 fix_dependencies = true;
4325 else
4327 Form_pg_inherits inhForm = (Form_pg_inherits) GETSTRUCT(tuple);
4329 if (parentOid == InvalidOid)
4332 * There exists a pg_inherits row, which we want to clear; do so.
4334 CatalogTupleDelete(pg_inherits, &tuple->t_self);
4335 fix_dependencies = true;
4337 else
4340 * A pg_inherits row exists. If it's the same we want, then we're
4341 * good; if it differs, that amounts to a corrupt catalog and
4342 * should not happen.
4344 if (inhForm->inhparent != parentOid)
4346 /* unexpected: we should not get called in this case */
4347 elog(ERROR, "bogus pg_inherit row: inhrelid %u inhparent %u",
4348 inhForm->inhrelid, inhForm->inhparent);
4351 /* already in the right state */
4352 fix_dependencies = false;
4356 /* done with pg_inherits */
4357 systable_endscan(scan);
4358 relation_close(pg_inherits, RowExclusiveLock);
4360 /* set relhassubclass if an index partition has been added to the parent */
4361 if (OidIsValid(parentOid))
4363 LockRelationOid(parentOid, ShareUpdateExclusiveLock);
4364 SetRelationHasSubclass(parentOid, true);
4367 /* set relispartition correctly on the partition */
4368 update_relispartition(partRelid, OidIsValid(parentOid));
4370 if (fix_dependencies)
4373 * Insert/delete pg_depend rows. If setting a parent, add PARTITION
4374 * dependencies on the parent index and the table; if removing a
4375 * parent, delete PARTITION dependencies.
4377 if (OidIsValid(parentOid))
4379 ObjectAddress partIdx;
4380 ObjectAddress parentIdx;
4381 ObjectAddress partitionTbl;
4383 ObjectAddressSet(partIdx, RelationRelationId, partRelid);
4384 ObjectAddressSet(parentIdx, RelationRelationId, parentOid);
4385 ObjectAddressSet(partitionTbl, RelationRelationId,
4386 partitionIdx->rd_index->indrelid);
4387 recordDependencyOn(&partIdx, &parentIdx,
4388 DEPENDENCY_PARTITION_PRI);
4389 recordDependencyOn(&partIdx, &partitionTbl,
4390 DEPENDENCY_PARTITION_SEC);
4392 else
4394 deleteDependencyRecordsForClass(RelationRelationId, partRelid,
4395 RelationRelationId,
4396 DEPENDENCY_PARTITION_PRI);
4397 deleteDependencyRecordsForClass(RelationRelationId, partRelid,
4398 RelationRelationId,
4399 DEPENDENCY_PARTITION_SEC);
4402 /* make our updates visible */
4403 CommandCounterIncrement();
4408 * Subroutine of IndexSetParentIndex to update the relispartition flag of the
4409 * given index to the given value.
4411 static void
4412 update_relispartition(Oid relationId, bool newval)
4414 HeapTuple tup;
4415 Relation classRel;
4417 classRel = table_open(RelationRelationId, RowExclusiveLock);
4418 tup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relationId));
4419 if (!HeapTupleIsValid(tup))
4420 elog(ERROR, "cache lookup failed for relation %u", relationId);
4421 Assert(((Form_pg_class) GETSTRUCT(tup))->relispartition != newval);
4422 ((Form_pg_class) GETSTRUCT(tup))->relispartition = newval;
4423 CatalogTupleUpdate(classRel, &tup->t_self, tup);
4424 heap_freetuple(tup);
4425 table_close(classRel, RowExclusiveLock);
4429 * Set the PROC_IN_SAFE_IC flag in MyProc->statusFlags.
4431 * When doing concurrent index builds, we can set this flag
4432 * to tell other processes concurrently running CREATE
4433 * INDEX CONCURRENTLY or REINDEX CONCURRENTLY to ignore us when
4434 * doing their waits for concurrent snapshots. On one hand it
4435 * avoids pointlessly waiting for a process that's not interesting
4436 * anyway; but more importantly it avoids deadlocks in some cases.
4438 * This can be done safely only for indexes that don't execute any
4439 * expressions that could access other tables, so index must not be
4440 * expressional nor partial. Caller is responsible for only calling
4441 * this routine when that assumption holds true.
4443 * (The flag is reset automatically at transaction end, so it must be
4444 * set for each transaction.)
4446 static inline void
4447 set_indexsafe_procflags(void)
4450 * This should only be called before installing xid or xmin in MyProc;
4451 * otherwise, concurrent processes could see an Xmin that moves backwards.
4453 Assert(MyProc->xid == InvalidTransactionId &&
4454 MyProc->xmin == InvalidTransactionId);
4456 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
4457 MyProc->statusFlags |= PROC_IN_SAFE_IC;
4458 ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags;
4459 LWLockRelease(ProcArrayLock);