PostgreSQL Source Code git master
indexcmds.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * indexcmds.c
4 * POSTGRES define and remove index code.
5 *
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/commands/indexcmds.c
12 *
13 *-------------------------------------------------------------------------
14 */
15
16#include "postgres.h"
17
18#include "access/amapi.h"
19#include "access/gist.h"
20#include "access/heapam.h"
21#include "access/htup_details.h"
22#include "access/reloptions.h"
23#include "access/sysattr.h"
24#include "access/tableam.h"
25#include "access/xact.h"
26#include "catalog/catalog.h"
27#include "catalog/index.h"
28#include "catalog/indexing.h"
29#include "catalog/namespace.h"
30#include "catalog/pg_am.h"
31#include "catalog/pg_authid.h"
34#include "catalog/pg_database.h"
35#include "catalog/pg_inherits.h"
37#include "catalog/pg_opclass.h"
38#include "catalog/pg_opfamily.h"
40#include "catalog/pg_type.h"
41#include "commands/comment.h"
42#include "commands/dbcommands.h"
43#include "commands/defrem.h"
45#include "commands/progress.h"
46#include "commands/tablecmds.h"
47#include "commands/tablespace.h"
48#include "mb/pg_wchar.h"
49#include "miscadmin.h"
50#include "nodes/makefuncs.h"
51#include "nodes/nodeFuncs.h"
52#include "optimizer/optimizer.h"
53#include "parser/parse_coerce.h"
54#include "parser/parse_oper.h"
57#include "pgstat.h"
59#include "storage/lmgr.h"
60#include "storage/proc.h"
61#include "storage/procarray.h"
62#include "utils/acl.h"
63#include "utils/builtins.h"
64#include "utils/fmgroids.h"
65#include "utils/guc.h"
67#include "utils/inval.h"
68#include "utils/lsyscache.h"
69#include "utils/memutils.h"
70#include "utils/partcache.h"
71#include "utils/pg_rusage.h"
72#include "utils/regproc.h"
73#include "utils/snapmgr.h"
74#include "utils/syscache.h"
75
76
77/* non-export function prototypes */
78static bool CompareOpclassOptions(const Datum *opts1, const Datum *opts2, int natts);
79static void CheckPredicate(Expr *predicate);
80static void ComputeIndexAttrs(IndexInfo *indexInfo,
81 Oid *typeOids,
82 Oid *collationOids,
83 Oid *opclassOids,
84 Datum *opclassOptions,
85 int16 *colOptions,
86 const List *attList,
87 const List *exclusionOpNames,
88 Oid relId,
89 const char *accessMethodName,
90 Oid accessMethodId,
91 bool amcanorder,
92 bool isconstraint,
93 bool iswithoutoverlaps,
94 Oid ddl_userid,
95 int ddl_sec_context,
96 int *ddl_save_nestlevel);
97static char *ChooseIndexName(const char *tabname, Oid namespaceId,
98 const List *colnames, const List *exclusionOpNames,
99 bool primary, bool isconstraint);
100static char *ChooseIndexNameAddition(const List *colnames);
101static List *ChooseIndexColumnNames(const List *indexElems);
102static void ReindexIndex(const ReindexStmt *stmt, const ReindexParams *params,
103 bool isTopLevel);
104static void RangeVarCallbackForReindexIndex(const RangeVar *relation,
105 Oid relId, Oid oldRelId, void *arg);
106static Oid ReindexTable(const ReindexStmt *stmt, const ReindexParams *params,
107 bool isTopLevel);
108static void ReindexMultipleTables(const ReindexStmt *stmt,
109 const ReindexParams *params);
110static void reindex_error_callback(void *arg);
111static void ReindexPartitions(const ReindexStmt *stmt, Oid relid,
112 const ReindexParams *params, bool isTopLevel);
113static void ReindexMultipleInternal(const ReindexStmt *stmt, const List *relids,
114 const ReindexParams *params);
116 Oid relationOid,
117 const ReindexParams *params);
118static void update_relispartition(Oid relationId, bool newval);
119static inline void set_indexsafe_procflags(void);
120
121/*
122 * callback argument type for RangeVarCallbackForReindexIndex()
123 */
125{
126 ReindexParams params; /* options from statement */
127 Oid locked_table_oid; /* tracks previously locked table */
128};
129
130/*
131 * callback arguments for reindex_error_callback()
132 */
133typedef struct ReindexErrorInfo
134{
135 char *relname;
139
140/*
141 * CheckIndexCompatible
142 * Determine whether an existing index definition is compatible with a
143 * prospective index definition, such that the existing index storage
144 * could become the storage of the new index, avoiding a rebuild.
145 *
146 * 'oldId': the OID of the existing index
147 * 'accessMethodName': name of the AM to use.
148 * 'attributeList': a list of IndexElem specifying columns and expressions
149 * to index on.
150 * 'exclusionOpNames': list of names of exclusion-constraint operators,
151 * or NIL if not an exclusion constraint.
152 * 'isWithoutOverlaps': true iff this index has a WITHOUT OVERLAPS clause.
153 *
154 * This is tailored to the needs of ALTER TABLE ALTER TYPE, which recreates
155 * any indexes that depended on a changing column from their pg_get_indexdef
156 * or pg_get_constraintdef definitions. We omit some of the sanity checks of
157 * DefineIndex. We assume that the old and new indexes have the same number
158 * of columns and that if one has an expression column or predicate, both do.
159 * Errors arising from the attribute list still apply.
160 *
161 * Most column type changes that can skip a table rewrite do not invalidate
162 * indexes. We acknowledge this when all operator classes, collations and
163 * exclusion operators match. Though we could further permit intra-opfamily
164 * changes for btree and hash indexes, that adds subtle complexity with no
165 * concrete benefit for core types. Note, that INCLUDE columns aren't
166 * checked by this function, for them it's enough that table rewrite is
167 * skipped.
168 *
169 * When a comparison or exclusion operator has a polymorphic input type, the
170 * actual input types must also match. This defends against the possibility
171 * that operators could vary behavior in response to get_fn_expr_argtype().
172 * At present, this hazard is theoretical: check_exclusion_constraint() and
173 * all core index access methods decline to set fn_expr for such calls.
174 *
175 * We do not yet implement a test to verify compatibility of expression
176 * columns or predicates, so assume any such index is incompatible.
177 */
178bool
180 const char *accessMethodName,
181 const List *attributeList,
182 const List *exclusionOpNames,
183 bool isWithoutOverlaps)
184{
185 bool isconstraint;
186 Oid *typeIds;
187 Oid *collationIds;
188 Oid *opclassIds;
189 Datum *opclassOptions;
190 Oid accessMethodId;
191 Oid relationId;
192 HeapTuple tuple;
193 Form_pg_index indexForm;
194 Form_pg_am accessMethodForm;
195 IndexAmRoutine *amRoutine;
196 bool amcanorder;
197 bool amsummarizing;
198 int16 *coloptions;
199 IndexInfo *indexInfo;
200 int numberOfAttributes;
201 int old_natts;
202 bool ret = true;
203 oidvector *old_indclass;
204 oidvector *old_indcollation;
205 Relation irel;
206 int i;
207 Datum d;
208
209 /* Caller should already have the relation locked in some way. */
210 relationId = IndexGetRelation(oldId, false);
211
212 /*
213 * We can pretend isconstraint = false unconditionally. It only serves to
214 * decide the text of an error message that should never happen for us.
215 */
216 isconstraint = false;
217
218 numberOfAttributes = list_length(attributeList);
219 Assert(numberOfAttributes > 0);
220 Assert(numberOfAttributes <= INDEX_MAX_KEYS);
221
222 /* look up the access method */
223 tuple = SearchSysCache1(AMNAME, PointerGetDatum(accessMethodName));
224 if (!HeapTupleIsValid(tuple))
226 (errcode(ERRCODE_UNDEFINED_OBJECT),
227 errmsg("access method \"%s\" does not exist",
228 accessMethodName)));
229 accessMethodForm = (Form_pg_am) GETSTRUCT(tuple);
230 accessMethodId = accessMethodForm->oid;
231 amRoutine = GetIndexAmRoutine(accessMethodForm->amhandler);
232 ReleaseSysCache(tuple);
233
234 amcanorder = amRoutine->amcanorder;
235 amsummarizing = amRoutine->amsummarizing;
236
237 /*
238 * Compute the operator classes, collations, and exclusion operators for
239 * the new index, so we can test whether it's compatible with the existing
240 * one. Note that ComputeIndexAttrs might fail here, but that's OK:
241 * DefineIndex would have failed later. Our attributeList contains only
242 * key attributes, thus we're filling ii_NumIndexAttrs and
243 * ii_NumIndexKeyAttrs with same value.
244 */
245 indexInfo = makeIndexInfo(numberOfAttributes, numberOfAttributes,
246 accessMethodId, NIL, NIL, false, false,
247 false, false, amsummarizing, isWithoutOverlaps);
248 typeIds = palloc_array(Oid, numberOfAttributes);
249 collationIds = palloc_array(Oid, numberOfAttributes);
250 opclassIds = palloc_array(Oid, numberOfAttributes);
251 opclassOptions = palloc_array(Datum, numberOfAttributes);
252 coloptions = palloc_array(int16, numberOfAttributes);
253 ComputeIndexAttrs(indexInfo,
254 typeIds, collationIds, opclassIds, opclassOptions,
255 coloptions, attributeList,
256 exclusionOpNames, relationId,
257 accessMethodName, accessMethodId,
258 amcanorder, isconstraint, isWithoutOverlaps, InvalidOid,
259 0, NULL);
260
261 /* Get the soon-obsolete pg_index tuple. */
262 tuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(oldId));
263 if (!HeapTupleIsValid(tuple))
264 elog(ERROR, "cache lookup failed for index %u", oldId);
265 indexForm = (Form_pg_index) GETSTRUCT(tuple);
266
267 /*
268 * We don't assess expressions or predicates; assume incompatibility.
269 * Also, if the index is invalid for any reason, treat it as incompatible.
270 */
271 if (!(heap_attisnull(tuple, Anum_pg_index_indpred, NULL) &&
272 heap_attisnull(tuple, Anum_pg_index_indexprs, NULL) &&
273 indexForm->indisvalid))
274 {
275 ReleaseSysCache(tuple);
276 return false;
277 }
278
279 /* Any change in operator class or collation breaks compatibility. */
280 old_natts = indexForm->indnkeyatts;
281 Assert(old_natts == numberOfAttributes);
282
283 d = SysCacheGetAttrNotNull(INDEXRELID, tuple, Anum_pg_index_indcollation);
284 old_indcollation = (oidvector *) DatumGetPointer(d);
285
286 d = SysCacheGetAttrNotNull(INDEXRELID, tuple, Anum_pg_index_indclass);
287 old_indclass = (oidvector *) DatumGetPointer(d);
288
289 ret = (memcmp(old_indclass->values, opclassIds, old_natts * sizeof(Oid)) == 0 &&
290 memcmp(old_indcollation->values, collationIds, old_natts * sizeof(Oid)) == 0);
291
292 ReleaseSysCache(tuple);
293
294 if (!ret)
295 return false;
296
297 /* For polymorphic opcintype, column type changes break compatibility. */
298 irel = index_open(oldId, AccessShareLock); /* caller probably has a lock */
299 for (i = 0; i < old_natts; i++)
300 {
301 if (IsPolymorphicType(get_opclass_input_type(opclassIds[i])) &&
302 TupleDescAttr(irel->rd_att, i)->atttypid != typeIds[i])
303 {
304 ret = false;
305 break;
306 }
307 }
308
309 /* Any change in opclass options break compatibility. */
310 if (ret)
311 {
312 Datum *oldOpclassOptions = palloc_array(Datum, old_natts);
313
314 for (i = 0; i < old_natts; i++)
315 oldOpclassOptions[i] = get_attoptions(oldId, i + 1);
316
317 ret = CompareOpclassOptions(oldOpclassOptions, opclassOptions, old_natts);
318
319 pfree(oldOpclassOptions);
320 }
321
322 /* Any change in exclusion operator selections breaks compatibility. */
323 if (ret && indexInfo->ii_ExclusionOps != NULL)
324 {
325 Oid *old_operators,
326 *old_procs;
327 uint16 *old_strats;
328
329 RelationGetExclusionInfo(irel, &old_operators, &old_procs, &old_strats);
330 ret = memcmp(old_operators, indexInfo->ii_ExclusionOps,
331 old_natts * sizeof(Oid)) == 0;
332
333 /* Require an exact input type match for polymorphic operators. */
334 if (ret)
335 {
336 for (i = 0; i < old_natts && ret; i++)
337 {
338 Oid left,
339 right;
340
341 op_input_types(indexInfo->ii_ExclusionOps[i], &left, &right);
342 if ((IsPolymorphicType(left) || IsPolymorphicType(right)) &&
343 TupleDescAttr(irel->rd_att, i)->atttypid != typeIds[i])
344 {
345 ret = false;
346 break;
347 }
348 }
349 }
350 }
351
352 index_close(irel, NoLock);
353 return ret;
354}
355
356/*
357 * CompareOpclassOptions
358 *
359 * Compare per-column opclass options which are represented by arrays of text[]
360 * datums. Both elements of arrays and array themselves can be NULL.
361 */
362static bool
363CompareOpclassOptions(const Datum *opts1, const Datum *opts2, int natts)
364{
365 int i;
366 FmgrInfo fm;
367
368 if (!opts1 && !opts2)
369 return true;
370
371 fmgr_info(F_ARRAY_EQ, &fm);
372 for (i = 0; i < natts; i++)
373 {
374 Datum opt1 = opts1 ? opts1[i] : (Datum) 0;
375 Datum opt2 = opts2 ? opts2[i] : (Datum) 0;
376
377 if (opt1 == (Datum) 0)
378 {
379 if (opt2 == (Datum) 0)
380 continue;
381 else
382 return false;
383 }
384 else if (opt2 == (Datum) 0)
385 return false;
386
387 /*
388 * Compare non-NULL text[] datums. Use C collation to enforce binary
389 * equivalence of texts, because we don't know anything about the
390 * semantics of opclass options.
391 */
392 if (!DatumGetBool(FunctionCall2Coll(&fm, C_COLLATION_OID, opt1, opt2)))
393 return false;
394 }
395
396 return true;
397}
398
399/*
400 * WaitForOlderSnapshots
401 *
402 * Wait for transactions that might have an older snapshot than the given xmin
403 * limit, because it might not contain tuples deleted just before it has
404 * been taken. Obtain a list of VXIDs of such transactions, and wait for them
405 * individually. This is used when building an index concurrently.
406 *
407 * We can exclude any running transactions that have xmin > the xmin given;
408 * their oldest snapshot must be newer than our xmin limit.
409 * We can also exclude any transactions that have xmin = zero, since they
410 * evidently have no live snapshot at all (and any one they might be in
411 * process of taking is certainly newer than ours). Transactions in other
412 * DBs can be ignored too, since they'll never even be able to see the
413 * index being worked on.
414 *
415 * We can also exclude autovacuum processes and processes running manual
416 * lazy VACUUMs, because they won't be fazed by missing index entries
417 * either. (Manual ANALYZEs, however, can't be excluded because they
418 * might be within transactions that are going to do arbitrary operations
419 * later.) Processes running CREATE INDEX CONCURRENTLY or REINDEX CONCURRENTLY
420 * on indexes that are neither expressional nor partial are also safe to
421 * ignore, since we know that those processes won't examine any data
422 * outside the table they're indexing.
423 *
424 * Also, GetCurrentVirtualXIDs never reports our own vxid, so we need not
425 * check for that.
426 *
427 * If a process goes idle-in-transaction with xmin zero, we do not need to
428 * wait for it anymore, per the above argument. We do not have the
429 * infrastructure right now to stop waiting if that happens, but we can at
430 * least avoid the folly of waiting when it is idle at the time we would
431 * begin to wait. We do this by repeatedly rechecking the output of
432 * GetCurrentVirtualXIDs. If, during any iteration, a particular vxid
433 * doesn't show up in the output, we know we can forget about it.
434 */
435void
437{
438 int n_old_snapshots;
439 int i;
440 VirtualTransactionId *old_snapshots;
441
442 old_snapshots = GetCurrentVirtualXIDs(limitXmin, true, false,
445 &n_old_snapshots);
446 if (progress)
448
449 for (i = 0; i < n_old_snapshots; i++)
450 {
451 if (!VirtualTransactionIdIsValid(old_snapshots[i]))
452 continue; /* found uninteresting in previous cycle */
453
454 if (i > 0)
455 {
456 /* see if anything's changed ... */
457 VirtualTransactionId *newer_snapshots;
458 int n_newer_snapshots;
459 int j;
460 int k;
461
462 newer_snapshots = GetCurrentVirtualXIDs(limitXmin,
463 true, false,
466 &n_newer_snapshots);
467 for (j = i; j < n_old_snapshots; j++)
468 {
469 if (!VirtualTransactionIdIsValid(old_snapshots[j]))
470 continue; /* found uninteresting in previous cycle */
471 for (k = 0; k < n_newer_snapshots; k++)
472 {
473 if (VirtualTransactionIdEquals(old_snapshots[j],
474 newer_snapshots[k]))
475 break;
476 }
477 if (k >= n_newer_snapshots) /* not there anymore */
478 SetInvalidVirtualTransactionId(old_snapshots[j]);
479 }
480 pfree(newer_snapshots);
481 }
482
483 if (VirtualTransactionIdIsValid(old_snapshots[i]))
484 {
485 /* If requested, publish who we're going to wait for. */
486 if (progress)
487 {
488 PGPROC *holder = ProcNumberGetProc(old_snapshots[i].procNumber);
489
490 if (holder)
492 holder->pid);
493 }
494 VirtualXactLock(old_snapshots[i], true);
495 }
496
497 if (progress)
499 }
500}
501
502
503/*
504 * DefineIndex
505 * Creates a new index.
506 *
507 * This function manages the current userid according to the needs of pg_dump.
508 * Recreating old-database catalog entries in new-database is fine, regardless
509 * of which users would have permission to recreate those entries now. That's
510 * just preservation of state. Running opaque expressions, like calling a
511 * function named in a catalog entry or evaluating a pg_node_tree in a catalog
512 * entry, as anyone other than the object owner, is not fine. To adhere to
513 * those principles and to remain fail-safe, use the table owner userid for
514 * most ACL checks. Use the original userid for ACL checks reached without
515 * traversing opaque expressions. (pg_dump can predict such ACL checks from
516 * catalogs.) Overall, this is a mess. Future DDL development should
517 * consider offering one DDL command for catalog setup and a separate DDL
518 * command for steps that run opaque expressions.
519 *
520 * 'tableId': the OID of the table relation on which the index is to be
521 * created
522 * 'stmt': IndexStmt describing the properties of the new index.
523 * 'indexRelationId': normally InvalidOid, but during bootstrap can be
524 * nonzero to specify a preselected OID for the index.
525 * 'parentIndexId': the OID of the parent index; InvalidOid if not the child
526 * of a partitioned index.
527 * 'parentConstraintId': the OID of the parent constraint; InvalidOid if not
528 * the child of a constraint (only used when recursing)
529 * 'total_parts': total number of direct and indirect partitions of relation;
530 * pass -1 if not known or rel is not partitioned.
531 * 'is_alter_table': this is due to an ALTER rather than a CREATE operation.
532 * 'check_rights': check for CREATE rights in namespace and tablespace. (This
533 * should be true except when ALTER is deleting/recreating an index.)
534 * 'check_not_in_use': check for table not already in use in current session.
535 * This should be true unless caller is holding the table open, in which
536 * case the caller had better have checked it earlier.
537 * 'skip_build': make the catalog entries but don't create the index files
538 * 'quiet': suppress the NOTICE chatter ordinarily provided for constraints.
539 *
540 * Returns the object address of the created index.
541 */
545 Oid indexRelationId,
546 Oid parentIndexId,
547 Oid parentConstraintId,
548 int total_parts,
549 bool is_alter_table,
550 bool check_rights,
551 bool check_not_in_use,
552 bool skip_build,
553 bool quiet)
554{
555 bool concurrent;
556 char *indexRelationName;
557 char *accessMethodName;
558 Oid *typeIds;
559 Oid *collationIds;
560 Oid *opclassIds;
561 Datum *opclassOptions;
562 Oid accessMethodId;
563 Oid namespaceId;
564 Oid tablespaceId;
565 Oid createdConstraintId = InvalidOid;
566 List *indexColNames;
567 List *allIndexParams;
568 Relation rel;
569 HeapTuple tuple;
570 Form_pg_am accessMethodForm;
571 IndexAmRoutine *amRoutine;
572 bool amcanorder;
573 bool amissummarizing;
574 amoptions_function amoptions;
575 bool exclusion;
576 bool partitioned;
577 bool safe_index;
578 Datum reloptions;
579 int16 *coloptions;
580 IndexInfo *indexInfo;
581 bits16 flags;
582 bits16 constr_flags;
583 int numberOfAttributes;
584 int numberOfKeyAttributes;
585 TransactionId limitXmin;
586 ObjectAddress address;
587 LockRelId heaprelid;
588 LOCKTAG heaplocktag;
589 LOCKMODE lockmode;
590 Snapshot snapshot;
591 Oid root_save_userid;
592 int root_save_sec_context;
593 int root_save_nestlevel;
594
595 root_save_nestlevel = NewGUCNestLevel();
596
598
599 /*
600 * Some callers need us to run with an empty default_tablespace; this is a
601 * necessary hack to be able to reproduce catalog state accurately when
602 * recreating indexes after table-rewriting ALTER TABLE.
603 */
604 if (stmt->reset_default_tblspc)
605 (void) set_config_option("default_tablespace", "",
607 GUC_ACTION_SAVE, true, 0, false);
608
609 /*
610 * Force non-concurrent build on temporary relations, even if CONCURRENTLY
611 * was requested. Other backends can't access a temporary relation, so
612 * there's no harm in grabbing a stronger lock, and a non-concurrent DROP
613 * is more efficient. Do this before any use of the concurrent option is
614 * done.
615 */
616 if (stmt->concurrent && get_rel_persistence(tableId) != RELPERSISTENCE_TEMP)
617 concurrent = true;
618 else
619 concurrent = false;
620
621 /*
622 * Start progress report. If we're building a partition, this was already
623 * done.
624 */
625 if (!OidIsValid(parentIndexId))
626 {
629 concurrent ?
632 }
633
634 /*
635 * No index OID to report yet
636 */
638 InvalidOid);
639
640 /*
641 * count key attributes in index
642 */
643 numberOfKeyAttributes = list_length(stmt->indexParams);
644
645 /*
646 * Calculate the new list of index columns including both key columns and
647 * INCLUDE columns. Later we can determine which of these are key
648 * columns, and which are just part of the INCLUDE list by checking the
649 * list position. A list item in a position less than ii_NumIndexKeyAttrs
650 * is part of the key columns, and anything equal to and over is part of
651 * the INCLUDE columns.
652 */
653 allIndexParams = list_concat_copy(stmt->indexParams,
654 stmt->indexIncludingParams);
655 numberOfAttributes = list_length(allIndexParams);
656
657 if (numberOfKeyAttributes <= 0)
659 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
660 errmsg("must specify at least one column")));
661 if (numberOfAttributes > INDEX_MAX_KEYS)
663 (errcode(ERRCODE_TOO_MANY_COLUMNS),
664 errmsg("cannot use more than %d columns in an index",
666
667 /*
668 * Only SELECT ... FOR UPDATE/SHARE are allowed while doing a standard
669 * index build; but for concurrent builds we allow INSERT/UPDATE/DELETE
670 * (but not VACUUM).
671 *
672 * NB: Caller is responsible for making sure that tableId refers to the
673 * relation on which the index should be built; except in bootstrap mode,
674 * this will typically require the caller to have already locked the
675 * relation. To avoid lock upgrade hazards, that lock should be at least
676 * as strong as the one we take here.
677 *
678 * NB: If the lock strength here ever changes, code that is run by
679 * parallel workers under the control of certain particular ambuild
680 * functions will need to be updated, too.
681 */
682 lockmode = concurrent ? ShareUpdateExclusiveLock : ShareLock;
683 rel = table_open(tableId, lockmode);
684
685 /*
686 * Switch to the table owner's userid, so that any index functions are run
687 * as that user. Also lock down security-restricted operations. We
688 * already arranged to make GUC variable changes local to this command.
689 */
690 GetUserIdAndSecContext(&root_save_userid, &root_save_sec_context);
691 SetUserIdAndSecContext(rel->rd_rel->relowner,
692 root_save_sec_context | SECURITY_RESTRICTED_OPERATION);
693
694 namespaceId = RelationGetNamespace(rel);
695
696 /*
697 * It has exclusion constraint behavior if it's an EXCLUDE constraint or a
698 * temporal PRIMARY KEY/UNIQUE constraint
699 */
700 exclusion = stmt->excludeOpNames || stmt->iswithoutoverlaps;
701
702 /* Ensure that it makes sense to index this kind of relation */
703 switch (rel->rd_rel->relkind)
704 {
705 case RELKIND_RELATION:
706 case RELKIND_MATVIEW:
707 case RELKIND_PARTITIONED_TABLE:
708 /* OK */
709 break;
710 default:
712 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
713 errmsg("cannot create index on relation \"%s\"",
716 break;
717 }
718
719 /*
720 * Establish behavior for partitioned tables, and verify sanity of
721 * parameters.
722 *
723 * We do not build an actual index in this case; we only create a few
724 * catalog entries. The actual indexes are built by recursing for each
725 * partition.
726 */
727 partitioned = rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE;
728 if (partitioned)
729 {
730 /*
731 * Note: we check 'stmt->concurrent' rather than 'concurrent', so that
732 * the error is thrown also for temporary tables. Seems better to be
733 * consistent, even though we could do it on temporary table because
734 * we're not actually doing it concurrently.
735 */
736 if (stmt->concurrent)
738 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
739 errmsg("cannot create index on partitioned table \"%s\" concurrently",
741 }
742
743 /*
744 * Don't try to CREATE INDEX on temp tables of other backends.
745 */
746 if (RELATION_IS_OTHER_TEMP(rel))
748 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
749 errmsg("cannot create indexes on temporary tables of other sessions")));
750
751 /*
752 * Unless our caller vouches for having checked this already, insist that
753 * the table not be in use by our own session, either. Otherwise we might
754 * fail to make entries in the new index (for instance, if an INSERT or
755 * UPDATE is in progress and has already made its list of target indexes).
756 */
757 if (check_not_in_use)
758 CheckTableNotInUse(rel, "CREATE INDEX");
759
760 /*
761 * Verify we (still) have CREATE rights in the rel's namespace.
762 * (Presumably we did when the rel was created, but maybe not anymore.)
763 * Skip check if caller doesn't want it. Also skip check if
764 * bootstrapping, since permissions machinery may not be working yet.
765 */
766 if (check_rights && !IsBootstrapProcessingMode())
767 {
768 AclResult aclresult;
769
770 aclresult = object_aclcheck(NamespaceRelationId, namespaceId, root_save_userid,
771 ACL_CREATE);
772 if (aclresult != ACLCHECK_OK)
773 aclcheck_error(aclresult, OBJECT_SCHEMA,
774 get_namespace_name(namespaceId));
775 }
776
777 /*
778 * Select tablespace to use. If not specified, use default tablespace
779 * (which may in turn default to database's default).
780 */
781 if (stmt->tableSpace)
782 {
783 tablespaceId = get_tablespace_oid(stmt->tableSpace, false);
784 if (partitioned && tablespaceId == MyDatabaseTableSpace)
786 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
787 errmsg("cannot specify default tablespace for partitioned relations")));
788 }
789 else
790 {
791 tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence,
792 partitioned);
793 /* note InvalidOid is OK in this case */
794 }
795
796 /* Check tablespace permissions */
797 if (check_rights &&
798 OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
799 {
800 AclResult aclresult;
801
802 aclresult = object_aclcheck(TableSpaceRelationId, tablespaceId, root_save_userid,
803 ACL_CREATE);
804 if (aclresult != ACLCHECK_OK)
806 get_tablespace_name(tablespaceId));
807 }
808
809 /*
810 * Force shared indexes into the pg_global tablespace. This is a bit of a
811 * hack but seems simpler than marking them in the BKI commands. On the
812 * other hand, if it's not shared, don't allow it to be placed there.
813 */
814 if (rel->rd_rel->relisshared)
815 tablespaceId = GLOBALTABLESPACE_OID;
816 else if (tablespaceId == GLOBALTABLESPACE_OID)
818 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
819 errmsg("only shared relations can be placed in pg_global tablespace")));
820
821 /*
822 * Choose the index column names.
823 */
824 indexColNames = ChooseIndexColumnNames(allIndexParams);
825
826 /*
827 * Select name for index if caller didn't specify
828 */
829 indexRelationName = stmt->idxname;
830 if (indexRelationName == NULL)
831 indexRelationName = ChooseIndexName(RelationGetRelationName(rel),
832 namespaceId,
833 indexColNames,
834 stmt->excludeOpNames,
835 stmt->primary,
836 stmt->isconstraint);
837
838 /*
839 * look up the access method, verify it can handle the requested features
840 */
841 accessMethodName = stmt->accessMethod;
842 tuple = SearchSysCache1(AMNAME, PointerGetDatum(accessMethodName));
843 if (!HeapTupleIsValid(tuple))
844 {
845 /*
846 * Hack to provide more-or-less-transparent updating of old RTREE
847 * indexes to GiST: if RTREE is requested and not found, use GIST.
848 */
849 if (strcmp(accessMethodName, "rtree") == 0)
850 {
852 (errmsg("substituting access method \"gist\" for obsolete method \"rtree\"")));
853 accessMethodName = "gist";
854 tuple = SearchSysCache1(AMNAME, PointerGetDatum(accessMethodName));
855 }
856
857 if (!HeapTupleIsValid(tuple))
859 (errcode(ERRCODE_UNDEFINED_OBJECT),
860 errmsg("access method \"%s\" does not exist",
861 accessMethodName)));
862 }
863 accessMethodForm = (Form_pg_am) GETSTRUCT(tuple);
864 accessMethodId = accessMethodForm->oid;
865 amRoutine = GetIndexAmRoutine(accessMethodForm->amhandler);
866
868 accessMethodId);
869
870 if (stmt->unique && !stmt->iswithoutoverlaps && !amRoutine->amcanunique)
872 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
873 errmsg("access method \"%s\" does not support unique indexes",
874 accessMethodName)));
875 if (stmt->indexIncludingParams != NIL && !amRoutine->amcaninclude)
877 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
878 errmsg("access method \"%s\" does not support included columns",
879 accessMethodName)));
880 if (numberOfKeyAttributes > 1 && !amRoutine->amcanmulticol)
882 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
883 errmsg("access method \"%s\" does not support multicolumn indexes",
884 accessMethodName)));
885 if (exclusion && amRoutine->amgettuple == NULL)
887 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
888 errmsg("access method \"%s\" does not support exclusion constraints",
889 accessMethodName)));
890 if (stmt->iswithoutoverlaps && strcmp(accessMethodName, "gist") != 0)
892 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
893 errmsg("access method \"%s\" does not support WITHOUT OVERLAPS constraints",
894 accessMethodName)));
895
896 amcanorder = amRoutine->amcanorder;
897 amoptions = amRoutine->amoptions;
898 amissummarizing = amRoutine->amsummarizing;
899
900 pfree(amRoutine);
901 ReleaseSysCache(tuple);
902
903 /*
904 * Validate predicate, if given
905 */
906 if (stmt->whereClause)
907 CheckPredicate((Expr *) stmt->whereClause);
908
909 /*
910 * Parse AM-specific options, convert to text array form, validate.
911 */
912 reloptions = transformRelOptions((Datum) 0, stmt->options,
913 NULL, NULL, false, false);
914
915 (void) index_reloptions(amoptions, reloptions, true);
916
917 /*
918 * Prepare arguments for index_create, primarily an IndexInfo structure.
919 * Note that predicates must be in implicit-AND format. In a concurrent
920 * build, mark it not-ready-for-inserts.
921 */
922 indexInfo = makeIndexInfo(numberOfAttributes,
923 numberOfKeyAttributes,
924 accessMethodId,
925 NIL, /* expressions, NIL for now */
926 make_ands_implicit((Expr *) stmt->whereClause),
927 stmt->unique,
928 stmt->nulls_not_distinct,
929 !concurrent,
930 concurrent,
931 amissummarizing,
932 stmt->iswithoutoverlaps);
933
934 typeIds = palloc_array(Oid, numberOfAttributes);
935 collationIds = palloc_array(Oid, numberOfAttributes);
936 opclassIds = palloc_array(Oid, numberOfAttributes);
937 opclassOptions = palloc_array(Datum, numberOfAttributes);
938 coloptions = palloc_array(int16, numberOfAttributes);
939 ComputeIndexAttrs(indexInfo,
940 typeIds, collationIds, opclassIds, opclassOptions,
941 coloptions, allIndexParams,
942 stmt->excludeOpNames, tableId,
943 accessMethodName, accessMethodId,
944 amcanorder, stmt->isconstraint, stmt->iswithoutoverlaps,
945 root_save_userid, root_save_sec_context,
946 &root_save_nestlevel);
947
948 /*
949 * Extra checks when creating a PRIMARY KEY index.
950 */
951 if (stmt->primary)
952 index_check_primary_key(rel, indexInfo, is_alter_table, stmt);
953
954 /*
955 * If this table is partitioned and we're creating a unique index, primary
956 * key, or exclusion constraint, make sure that the partition key is a
957 * subset of the index's columns. Otherwise it would be possible to
958 * violate uniqueness by putting values that ought to be unique in
959 * different partitions.
960 *
961 * We could lift this limitation if we had global indexes, but those have
962 * their own problems, so this is a useful feature combination.
963 */
964 if (partitioned && (stmt->unique || exclusion))
965 {
967 const char *constraint_type;
968 int i;
969
970 if (stmt->primary)
971 constraint_type = "PRIMARY KEY";
972 else if (stmt->unique)
973 constraint_type = "UNIQUE";
974 else if (stmt->excludeOpNames)
975 constraint_type = "EXCLUDE";
976 else
977 {
978 elog(ERROR, "unknown constraint type");
979 constraint_type = NULL; /* keep compiler quiet */
980 }
981
982 /*
983 * Verify that all the columns in the partition key appear in the
984 * unique key definition, with the same notion of equality.
985 */
986 for (i = 0; i < key->partnatts; i++)
987 {
988 bool found = false;
989 int eq_strategy;
990 Oid ptkey_eqop;
991 int j;
992
993 /*
994 * Identify the equality operator associated with this partkey
995 * column. For list and range partitioning, partkeys use btree
996 * operator classes; hash partitioning uses hash operator classes.
997 * (Keep this in sync with ComputePartitionAttrs!)
998 */
999 if (key->strategy == PARTITION_STRATEGY_HASH)
1000 eq_strategy = HTEqualStrategyNumber;
1001 else
1002 eq_strategy = BTEqualStrategyNumber;
1003
1004 ptkey_eqop = get_opfamily_member(key->partopfamily[i],
1005 key->partopcintype[i],
1006 key->partopcintype[i],
1007 eq_strategy);
1008 if (!OidIsValid(ptkey_eqop))
1009 elog(ERROR, "missing operator %d(%u,%u) in partition opfamily %u",
1010 eq_strategy, key->partopcintype[i], key->partopcintype[i],
1011 key->partopfamily[i]);
1012
1013 /*
1014 * We'll need to be able to identify the equality operators
1015 * associated with index columns, too. We know what to do with
1016 * btree opclasses; if there are ever any other index types that
1017 * support unique indexes, this logic will need extension. But if
1018 * we have an exclusion constraint (or a temporal PK), it already
1019 * knows the operators, so we don't have to infer them.
1020 */
1021 if (stmt->unique && !stmt->iswithoutoverlaps && accessMethodId != BTREE_AM_OID)
1022 ereport(ERROR,
1023 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1024 errmsg("cannot match partition key to an index using access method \"%s\"",
1025 accessMethodName)));
1026
1027 /*
1028 * It may be possible to support UNIQUE constraints when partition
1029 * keys are expressions, but is it worth it? Give up for now.
1030 */
1031 if (key->partattrs[i] == 0)
1032 ereport(ERROR,
1033 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1034 errmsg("unsupported %s constraint with partition key definition",
1035 constraint_type),
1036 errdetail("%s constraints cannot be used when partition keys include expressions.",
1037 constraint_type)));
1038
1039 /* Search the index column(s) for a match */
1040 for (j = 0; j < indexInfo->ii_NumIndexKeyAttrs; j++)
1041 {
1042 if (key->partattrs[i] == indexInfo->ii_IndexAttrNumbers[j])
1043 {
1044 /*
1045 * Matched the column, now what about the collation and
1046 * equality op?
1047 */
1048 Oid idx_opfamily;
1049 Oid idx_opcintype;
1050
1051 if (key->partcollation[i] != collationIds[j])
1052 continue;
1053
1054 if (get_opclass_opfamily_and_input_type(opclassIds[j],
1055 &idx_opfamily,
1056 &idx_opcintype))
1057 {
1058 Oid idx_eqop = InvalidOid;
1059
1060 if (stmt->unique && !stmt->iswithoutoverlaps)
1061 idx_eqop = get_opfamily_member(idx_opfamily,
1062 idx_opcintype,
1063 idx_opcintype,
1065 else if (exclusion)
1066 idx_eqop = indexInfo->ii_ExclusionOps[j];
1067 Assert(idx_eqop);
1068
1069 if (ptkey_eqop == idx_eqop)
1070 {
1071 found = true;
1072 break;
1073 }
1074 else if (exclusion)
1075 {
1076 /*
1077 * We found a match, but it's not an equality
1078 * operator. Instead of failing below with an
1079 * error message about a missing column, fail now
1080 * and explain that the operator is wrong.
1081 */
1082 Form_pg_attribute att = TupleDescAttr(RelationGetDescr(rel), key->partattrs[i] - 1);
1083
1084 ereport(ERROR,
1085 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1086 errmsg("cannot match partition key to index on column \"%s\" using non-equal operator \"%s\"",
1087 NameStr(att->attname),
1088 get_opname(indexInfo->ii_ExclusionOps[j]))));
1089 }
1090 }
1091 }
1092 }
1093
1094 if (!found)
1095 {
1097
1099 key->partattrs[i] - 1);
1100 ereport(ERROR,
1101 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1102 errmsg("unique constraint on partitioned table must include all partitioning columns"),
1103 errdetail("%s constraint on table \"%s\" lacks column \"%s\" which is part of the partition key.",
1104 constraint_type, RelationGetRelationName(rel),
1105 NameStr(att->attname))));
1106 }
1107 }
1108 }
1109
1110
1111 /*
1112 * We disallow indexes on system columns. They would not necessarily get
1113 * updated correctly, and they don't seem useful anyway.
1114 */
1115 for (int i = 0; i < indexInfo->ii_NumIndexAttrs; i++)
1116 {
1117 AttrNumber attno = indexInfo->ii_IndexAttrNumbers[i];
1118
1119 if (attno < 0)
1120 ereport(ERROR,
1121 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1122 errmsg("index creation on system columns is not supported")));
1123 }
1124
1125 /*
1126 * Also check for system columns used in expressions or predicates.
1127 */
1128 if (indexInfo->ii_Expressions || indexInfo->ii_Predicate)
1129 {
1130 Bitmapset *indexattrs = NULL;
1131
1132 pull_varattnos((Node *) indexInfo->ii_Expressions, 1, &indexattrs);
1133 pull_varattnos((Node *) indexInfo->ii_Predicate, 1, &indexattrs);
1134
1135 for (int i = FirstLowInvalidHeapAttributeNumber + 1; i < 0; i++)
1136 {
1138 indexattrs))
1139 ereport(ERROR,
1140 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1141 errmsg("index creation on system columns is not supported")));
1142 }
1143 }
1144
1145 /* Is index safe for others to ignore? See set_indexsafe_procflags() */
1146 safe_index = indexInfo->ii_Expressions == NIL &&
1147 indexInfo->ii_Predicate == NIL;
1148
1149 /*
1150 * Report index creation if appropriate (delay this till after most of the
1151 * error checks)
1152 */
1153 if (stmt->isconstraint && !quiet)
1154 {
1155 const char *constraint_type;
1156
1157 if (stmt->primary)
1158 constraint_type = "PRIMARY KEY";
1159 else if (stmt->unique)
1160 constraint_type = "UNIQUE";
1161 else if (stmt->excludeOpNames)
1162 constraint_type = "EXCLUDE";
1163 else
1164 {
1165 elog(ERROR, "unknown constraint type");
1166 constraint_type = NULL; /* keep compiler quiet */
1167 }
1168
1170 (errmsg_internal("%s %s will create implicit index \"%s\" for table \"%s\"",
1171 is_alter_table ? "ALTER TABLE / ADD" : "CREATE TABLE /",
1172 constraint_type,
1173 indexRelationName, RelationGetRelationName(rel))));
1174 }
1175
1176 /*
1177 * A valid stmt->oldNumber implies that we already have a built form of
1178 * the index. The caller should also decline any index build.
1179 */
1180 Assert(!RelFileNumberIsValid(stmt->oldNumber) || (skip_build && !concurrent));
1181
1182 /*
1183 * Make the catalog entries for the index, including constraints. This
1184 * step also actually builds the index, except if caller requested not to
1185 * or in concurrent mode, in which case it'll be done later, or doing a
1186 * partitioned index (because those don't have storage).
1187 */
1188 flags = constr_flags = 0;
1189 if (stmt->isconstraint)
1191 if (skip_build || concurrent || partitioned)
1192 flags |= INDEX_CREATE_SKIP_BUILD;
1193 if (stmt->if_not_exists)
1195 if (concurrent)
1196 flags |= INDEX_CREATE_CONCURRENT;
1197 if (partitioned)
1198 flags |= INDEX_CREATE_PARTITIONED;
1199 if (stmt->primary)
1200 flags |= INDEX_CREATE_IS_PRIMARY;
1201
1202 /*
1203 * If the table is partitioned, and recursion was declined but partitions
1204 * exist, mark the index as invalid.
1205 */
1206 if (partitioned && stmt->relation && !stmt->relation->inh)
1207 {
1209
1210 if (pd->nparts != 0)
1211 flags |= INDEX_CREATE_INVALID;
1212 }
1213
1214 if (stmt->deferrable)
1215 constr_flags |= INDEX_CONSTR_CREATE_DEFERRABLE;
1216 if (stmt->initdeferred)
1217 constr_flags |= INDEX_CONSTR_CREATE_INIT_DEFERRED;
1218 if (stmt->iswithoutoverlaps)
1220
1221 indexRelationId =
1222 index_create(rel, indexRelationName, indexRelationId, parentIndexId,
1223 parentConstraintId,
1224 stmt->oldNumber, indexInfo, indexColNames,
1225 accessMethodId, tablespaceId,
1226 collationIds, opclassIds, opclassOptions,
1227 coloptions, NULL, reloptions,
1228 flags, constr_flags,
1229 allowSystemTableMods, !check_rights,
1230 &createdConstraintId);
1231
1232 ObjectAddressSet(address, RelationRelationId, indexRelationId);
1233
1234 if (!OidIsValid(indexRelationId))
1235 {
1236 /*
1237 * Roll back any GUC changes executed by index functions. Also revert
1238 * to original default_tablespace if we changed it above.
1239 */
1240 AtEOXact_GUC(false, root_save_nestlevel);
1241
1242 /* Restore userid and security context */
1243 SetUserIdAndSecContext(root_save_userid, root_save_sec_context);
1244
1245 table_close(rel, NoLock);
1246
1247 /* If this is the top-level index, we're done */
1248 if (!OidIsValid(parentIndexId))
1250
1251 return address;
1252 }
1253
1254 /*
1255 * Roll back any GUC changes executed by index functions, and keep
1256 * subsequent changes local to this command. This is essential if some
1257 * index function changed a behavior-affecting GUC, e.g. search_path.
1258 */
1259 AtEOXact_GUC(false, root_save_nestlevel);
1260 root_save_nestlevel = NewGUCNestLevel();
1262
1263 /* Add any requested comment */
1264 if (stmt->idxcomment != NULL)
1265 CreateComments(indexRelationId, RelationRelationId, 0,
1266 stmt->idxcomment);
1267
1268 if (partitioned)
1269 {
1270 PartitionDesc partdesc;
1271
1272 /*
1273 * Unless caller specified to skip this step (via ONLY), process each
1274 * partition to make sure they all contain a corresponding index.
1275 *
1276 * If we're called internally (no stmt->relation), recurse always.
1277 */
1278 partdesc = RelationGetPartitionDesc(rel, true);
1279 if ((!stmt->relation || stmt->relation->inh) && partdesc->nparts > 0)
1280 {
1281 int nparts = partdesc->nparts;
1282 Oid *part_oids = palloc_array(Oid, nparts);
1283 bool invalidate_parent = false;
1284 Relation parentIndex;
1285 TupleDesc parentDesc;
1286
1287 /*
1288 * Report the total number of partitions at the start of the
1289 * command; don't update it when being called recursively.
1290 */
1291 if (!OidIsValid(parentIndexId))
1292 {
1293 /*
1294 * When called by ProcessUtilitySlow, the number of partitions
1295 * is passed in as an optimization; but other callers pass -1
1296 * since they don't have the value handy. This should count
1297 * partitions the same way, ie one less than the number of
1298 * relations find_all_inheritors reports.
1299 *
1300 * We assume we needn't ask find_all_inheritors to take locks,
1301 * because that should have happened already for all callers.
1302 * Even if it did not, this is safe as long as we don't try to
1303 * touch the partitions here; the worst consequence would be a
1304 * bogus progress-reporting total.
1305 */
1306 if (total_parts < 0)
1307 {
1308 List *children = find_all_inheritors(tableId, NoLock, NULL);
1309
1310 total_parts = list_length(children) - 1;
1311 list_free(children);
1312 }
1313
1315 total_parts);
1316 }
1317
1318 /* Make a local copy of partdesc->oids[], just for safety */
1319 memcpy(part_oids, partdesc->oids, sizeof(Oid) * nparts);
1320
1321 /*
1322 * We'll need an IndexInfo describing the parent index. The one
1323 * built above is almost good enough, but not quite, because (for
1324 * example) its predicate expression if any hasn't been through
1325 * expression preprocessing. The most reliable way to get an
1326 * IndexInfo that will match those for child indexes is to build
1327 * it the same way, using BuildIndexInfo().
1328 */
1329 parentIndex = index_open(indexRelationId, lockmode);
1330 indexInfo = BuildIndexInfo(parentIndex);
1331
1332 parentDesc = RelationGetDescr(rel);
1333
1334 /*
1335 * For each partition, scan all existing indexes; if one matches
1336 * our index definition and is not already attached to some other
1337 * parent index, attach it to the one we just created.
1338 *
1339 * If none matches, build a new index by calling ourselves
1340 * recursively with the same options (except for the index name).
1341 */
1342 for (int i = 0; i < nparts; i++)
1343 {
1344 Oid childRelid = part_oids[i];
1345 Relation childrel;
1346 Oid child_save_userid;
1347 int child_save_sec_context;
1348 int child_save_nestlevel;
1349 List *childidxs;
1350 ListCell *cell;
1351 AttrMap *attmap;
1352 bool found = false;
1353
1354 childrel = table_open(childRelid, lockmode);
1355
1356 GetUserIdAndSecContext(&child_save_userid,
1357 &child_save_sec_context);
1358 SetUserIdAndSecContext(childrel->rd_rel->relowner,
1359 child_save_sec_context | SECURITY_RESTRICTED_OPERATION);
1360 child_save_nestlevel = NewGUCNestLevel();
1362
1363 /*
1364 * Don't try to create indexes on foreign tables, though. Skip
1365 * those if a regular index, or fail if trying to create a
1366 * constraint index.
1367 */
1368 if (childrel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
1369 {
1370 if (stmt->unique || stmt->primary)
1371 ereport(ERROR,
1372 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1373 errmsg("cannot create unique index on partitioned table \"%s\"",
1375 errdetail("Table \"%s\" contains partitions that are foreign tables.",
1377
1378 AtEOXact_GUC(false, child_save_nestlevel);
1379 SetUserIdAndSecContext(child_save_userid,
1380 child_save_sec_context);
1381 table_close(childrel, lockmode);
1382 continue;
1383 }
1384
1385 childidxs = RelationGetIndexList(childrel);
1386 attmap =
1388 parentDesc,
1389 false);
1390
1391 foreach(cell, childidxs)
1392 {
1393 Oid cldidxid = lfirst_oid(cell);
1394 Relation cldidx;
1395 IndexInfo *cldIdxInfo;
1396
1397 /* this index is already partition of another one */
1398 if (has_superclass(cldidxid))
1399 continue;
1400
1401 cldidx = index_open(cldidxid, lockmode);
1402 cldIdxInfo = BuildIndexInfo(cldidx);
1403 if (CompareIndexInfo(cldIdxInfo, indexInfo,
1404 cldidx->rd_indcollation,
1405 parentIndex->rd_indcollation,
1406 cldidx->rd_opfamily,
1407 parentIndex->rd_opfamily,
1408 attmap))
1409 {
1410 Oid cldConstrOid = InvalidOid;
1411
1412 /*
1413 * Found a match.
1414 *
1415 * If this index is being created in the parent
1416 * because of a constraint, then the child needs to
1417 * have a constraint also, so look for one. If there
1418 * is no such constraint, this index is no good, so
1419 * keep looking.
1420 */
1421 if (createdConstraintId != InvalidOid)
1422 {
1423 cldConstrOid =
1425 cldidxid);
1426 if (cldConstrOid == InvalidOid)
1427 {
1428 index_close(cldidx, lockmode);
1429 continue;
1430 }
1431 }
1432
1433 /* Attach index to parent and we're done. */
1434 IndexSetParentIndex(cldidx, indexRelationId);
1435 if (createdConstraintId != InvalidOid)
1436 ConstraintSetParentConstraint(cldConstrOid,
1437 createdConstraintId,
1438 childRelid);
1439
1440 if (!cldidx->rd_index->indisvalid)
1441 invalidate_parent = true;
1442
1443 found = true;
1444
1445 /*
1446 * Report this partition as processed. Note that if
1447 * the partition has children itself, we'd ideally
1448 * count the children and update the progress report
1449 * for all of them; but that seems unduly expensive.
1450 * Instead, the progress report will act like all such
1451 * indirect children were processed in zero time at
1452 * the end of the command.
1453 */
1455
1456 /* keep lock till commit */
1457 index_close(cldidx, NoLock);
1458 break;
1459 }
1460
1461 index_close(cldidx, lockmode);
1462 }
1463
1464 list_free(childidxs);
1465 AtEOXact_GUC(false, child_save_nestlevel);
1466 SetUserIdAndSecContext(child_save_userid,
1467 child_save_sec_context);
1468 table_close(childrel, NoLock);
1469
1470 /*
1471 * If no matching index was found, create our own.
1472 */
1473 if (!found)
1474 {
1475 IndexStmt *childStmt;
1476 ObjectAddress childAddr;
1477
1478 /*
1479 * Build an IndexStmt describing the desired child index
1480 * in the same way that we do during ATTACH PARTITION.
1481 * Notably, we rely on generateClonedIndexStmt to produce
1482 * a search-path-independent representation, which the
1483 * original IndexStmt might not be.
1484 */
1485 childStmt = generateClonedIndexStmt(NULL,
1486 parentIndex,
1487 attmap,
1488 NULL);
1489
1490 /*
1491 * Recurse as the starting user ID. Callee will use that
1492 * for permission checks, then switch again.
1493 */
1494 Assert(GetUserId() == child_save_userid);
1495 SetUserIdAndSecContext(root_save_userid,
1496 root_save_sec_context);
1497 childAddr =
1498 DefineIndex(childRelid, childStmt,
1499 InvalidOid, /* no predefined OID */
1500 indexRelationId, /* this is our child */
1501 createdConstraintId,
1502 -1,
1503 is_alter_table, check_rights,
1504 check_not_in_use,
1505 skip_build, quiet);
1506 SetUserIdAndSecContext(child_save_userid,
1507 child_save_sec_context);
1508
1509 /*
1510 * Check if the index just created is valid or not, as it
1511 * could be possible that it has been switched as invalid
1512 * when recursing across multiple partition levels.
1513 */
1514 if (!get_index_isvalid(childAddr.objectId))
1515 invalidate_parent = true;
1516 }
1517
1518 free_attrmap(attmap);
1519 }
1520
1521 index_close(parentIndex, lockmode);
1522
1523 /*
1524 * The pg_index row we inserted for this index was marked
1525 * indisvalid=true. But if we attached an existing index that is
1526 * invalid, this is incorrect, so update our row to invalid too.
1527 */
1528 if (invalidate_parent)
1529 {
1530 Relation pg_index = table_open(IndexRelationId, RowExclusiveLock);
1531 HeapTuple tup,
1532 newtup;
1533
1534 tup = SearchSysCache1(INDEXRELID,
1535 ObjectIdGetDatum(indexRelationId));
1536 if (!HeapTupleIsValid(tup))
1537 elog(ERROR, "cache lookup failed for index %u",
1538 indexRelationId);
1539 newtup = heap_copytuple(tup);
1540 ((Form_pg_index) GETSTRUCT(newtup))->indisvalid = false;
1541 CatalogTupleUpdate(pg_index, &tup->t_self, newtup);
1542 ReleaseSysCache(tup);
1543 table_close(pg_index, RowExclusiveLock);
1544 heap_freetuple(newtup);
1545
1546 /*
1547 * CCI here to make this update visible, in case this recurses
1548 * across multiple partition levels.
1549 */
1551 }
1552 }
1553
1554 /*
1555 * Indexes on partitioned tables are not themselves built, so we're
1556 * done here.
1557 */
1558 AtEOXact_GUC(false, root_save_nestlevel);
1559 SetUserIdAndSecContext(root_save_userid, root_save_sec_context);
1560 table_close(rel, NoLock);
1561 if (!OidIsValid(parentIndexId))
1563 else
1564 {
1565 /* Update progress for an intermediate partitioned index itself */
1567 }
1568
1569 return address;
1570 }
1571
1572 AtEOXact_GUC(false, root_save_nestlevel);
1573 SetUserIdAndSecContext(root_save_userid, root_save_sec_context);
1574
1575 if (!concurrent)
1576 {
1577 /* Close the heap and we're done, in the non-concurrent case */
1578 table_close(rel, NoLock);
1579
1580 /*
1581 * If this is the top-level index, the command is done overall;
1582 * otherwise, increment progress to report one child index is done.
1583 */
1584 if (!OidIsValid(parentIndexId))
1586 else
1588
1589 return address;
1590 }
1591
1592 /* save lockrelid and locktag for below, then close rel */
1593 heaprelid = rel->rd_lockInfo.lockRelId;
1594 SET_LOCKTAG_RELATION(heaplocktag, heaprelid.dbId, heaprelid.relId);
1595 table_close(rel, NoLock);
1596
1597 /*
1598 * For a concurrent build, it's important to make the catalog entries
1599 * visible to other transactions before we start to build the index. That
1600 * will prevent them from making incompatible HOT updates. The new index
1601 * will be marked not indisready and not indisvalid, so that no one else
1602 * tries to either insert into it or use it for queries.
1603 *
1604 * We must commit our current transaction so that the index becomes
1605 * visible; then start another. Note that all the data structures we just
1606 * built are lost in the commit. The only data we keep past here are the
1607 * relation IDs.
1608 *
1609 * Before committing, get a session-level lock on the table, to ensure
1610 * that neither it nor the index can be dropped before we finish. This
1611 * cannot block, even if someone else is waiting for access, because we
1612 * already have the same lock within our transaction.
1613 *
1614 * Note: we don't currently bother with a session lock on the index,
1615 * because there are no operations that could change its state while we
1616 * hold lock on the parent table. This might need to change later.
1617 */
1619
1623
1624 /* Tell concurrent index builds to ignore us, if index qualifies */
1625 if (safe_index)
1627
1628 /*
1629 * The index is now visible, so we can report the OID. While on it,
1630 * include the report for the beginning of phase 2.
1631 */
1632 {
1633 const int progress_cols[] = {
1636 };
1637 const int64 progress_vals[] = {
1638 indexRelationId,
1640 };
1641
1642 pgstat_progress_update_multi_param(2, progress_cols, progress_vals);
1643 }
1644
1645 /*
1646 * Phase 2 of concurrent index build (see comments for validate_index()
1647 * for an overview of how this works)
1648 *
1649 * Now we must wait until no running transaction could have the table open
1650 * with the old list of indexes. Use ShareLock to consider running
1651 * transactions that hold locks that permit writing to the table. Note we
1652 * do not need to worry about xacts that open the table for writing after
1653 * this point; they will see the new index when they open it.
1654 *
1655 * Note: the reason we use actual lock acquisition here, rather than just
1656 * checking the ProcArray and sleeping, is that deadlock is possible if
1657 * one of the transactions in question is blocked trying to acquire an
1658 * exclusive lock on our table. The lock code will detect deadlock and
1659 * error out properly.
1660 */
1661 WaitForLockers(heaplocktag, ShareLock, true);
1662
1663 /*
1664 * At this moment we are sure that there are no transactions with the
1665 * table open for write that don't have this new index in their list of
1666 * indexes. We have waited out all the existing transactions and any new
1667 * transaction will have the new index in its list, but the index is still
1668 * marked as "not-ready-for-inserts". The index is consulted while
1669 * deciding HOT-safety though. This arrangement ensures that no new HOT
1670 * chains can be created where the new tuple and the old tuple in the
1671 * chain have different index keys.
1672 *
1673 * We now take a new snapshot, and build the index using all tuples that
1674 * are visible in this snapshot. We can be sure that any HOT updates to
1675 * these tuples will be compatible with the index, since any updates made
1676 * by transactions that didn't know about the index are now committed or
1677 * rolled back. Thus, each visible tuple is either the end of its
1678 * HOT-chain or the extension of the chain is HOT-safe for this index.
1679 */
1680
1681 /* Set ActiveSnapshot since functions in the indexes may need it */
1683
1684 /* Perform concurrent build of index */
1685 index_concurrently_build(tableId, indexRelationId);
1686
1687 /* we can do away with our snapshot */
1689
1690 /*
1691 * Commit this transaction to make the indisready update visible.
1692 */
1695
1696 /* Tell concurrent index builds to ignore us, if index qualifies */
1697 if (safe_index)
1699
1700 /*
1701 * Phase 3 of concurrent index build
1702 *
1703 * We once again wait until no transaction can have the table open with
1704 * the index marked as read-only for updates.
1705 */
1708 WaitForLockers(heaplocktag, ShareLock, true);
1709
1710 /*
1711 * Now take the "reference snapshot" that will be used by validate_index()
1712 * to filter candidate tuples. Beware! There might still be snapshots in
1713 * use that treat some transaction as in-progress that our reference
1714 * snapshot treats as committed. If such a recently-committed transaction
1715 * deleted tuples in the table, we will not include them in the index; yet
1716 * those transactions which see the deleting one as still-in-progress will
1717 * expect such tuples to be there once we mark the index as valid.
1718 *
1719 * We solve this by waiting for all endangered transactions to exit before
1720 * we mark the index as valid.
1721 *
1722 * We also set ActiveSnapshot to this snap, since functions in indexes may
1723 * need a snapshot.
1724 */
1726 PushActiveSnapshot(snapshot);
1727
1728 /*
1729 * Scan the index and the heap, insert any missing index entries.
1730 */
1731 validate_index(tableId, indexRelationId, snapshot);
1732
1733 /*
1734 * Drop the reference snapshot. We must do this before waiting out other
1735 * snapshot holders, else we will deadlock against other processes also
1736 * doing CREATE INDEX CONCURRENTLY, which would see our snapshot as one
1737 * they must wait for. But first, save the snapshot's xmin to use as
1738 * limitXmin for GetCurrentVirtualXIDs().
1739 */
1740 limitXmin = snapshot->xmin;
1741
1743 UnregisterSnapshot(snapshot);
1744
1745 /*
1746 * The snapshot subsystem could still contain registered snapshots that
1747 * are holding back our process's advertised xmin; in particular, if
1748 * default_transaction_isolation = serializable, there is a transaction
1749 * snapshot that is still active. The CatalogSnapshot is likewise a
1750 * hazard. To ensure no deadlocks, we must commit and start yet another
1751 * transaction, and do our wait before any snapshot has been taken in it.
1752 */
1755
1756 /* Tell concurrent index builds to ignore us, if index qualifies */
1757 if (safe_index)
1759
1760 /* We should now definitely not be advertising any xmin. */
1762
1763 /*
1764 * The index is now valid in the sense that it contains all currently
1765 * interesting tuples. But since it might not contain tuples deleted just
1766 * before the reference snap was taken, we have to wait out any
1767 * transactions that might have older snapshots.
1768 */
1771 WaitForOlderSnapshots(limitXmin, true);
1772
1773 /*
1774 * Updating pg_index might involve TOAST table access, so ensure we have a
1775 * valid snapshot.
1776 */
1778
1779 /*
1780 * Index can now be marked valid -- update its pg_index entry
1781 */
1783
1785
1786 /*
1787 * The pg_index update will cause backends (including this one) to update
1788 * relcache entries for the index itself, but we should also send a
1789 * relcache inval on the parent table to force replanning of cached plans.
1790 * Otherwise existing sessions might fail to use the new index where it
1791 * would be useful. (Note that our earlier commits did not create reasons
1792 * to replan; so relcache flush on the index itself was sufficient.)
1793 */
1795
1796 /*
1797 * Last thing to do is release the session-level lock on the parent table.
1798 */
1800
1802
1803 return address;
1804}
1805
1806
1807/*
1808 * CheckPredicate
1809 * Checks that the given partial-index predicate is valid.
1810 *
1811 * This used to also constrain the form of the predicate to forms that
1812 * indxpath.c could do something with. However, that seems overly
1813 * restrictive. One useful application of partial indexes is to apply
1814 * a UNIQUE constraint across a subset of a table, and in that scenario
1815 * any evaluable predicate will work. So accept any predicate here
1816 * (except ones requiring a plan), and let indxpath.c fend for itself.
1817 */
1818static void
1820{
1821 /*
1822 * transformExpr() should have already rejected subqueries, aggregates,
1823 * and window functions, based on the EXPR_KIND_ for a predicate.
1824 */
1825
1826 /*
1827 * A predicate using mutable functions is probably wrong, for the same
1828 * reasons that we don't allow an index expression to use one.
1829 */
1831 ereport(ERROR,
1832 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1833 errmsg("functions in index predicate must be marked IMMUTABLE")));
1834}
1835
1836/*
1837 * Compute per-index-column information, including indexed column numbers
1838 * or index expressions, opclasses and their options. Note, all output vectors
1839 * should be allocated for all columns, including "including" ones.
1840 *
1841 * If the caller switched to the table owner, ddl_userid is the role for ACL
1842 * checks reached without traversing opaque expressions. Otherwise, it's
1843 * InvalidOid, and other ddl_* arguments are undefined.
1844 */
1845static void
1847 Oid *typeOids,
1848 Oid *collationOids,
1849 Oid *opclassOids,
1850 Datum *opclassOptions,
1851 int16 *colOptions,
1852 const List *attList, /* list of IndexElem's */
1853 const List *exclusionOpNames,
1854 Oid relId,
1855 const char *accessMethodName,
1856 Oid accessMethodId,
1857 bool amcanorder,
1858 bool isconstraint,
1859 bool iswithoutoverlaps,
1860 Oid ddl_userid,
1861 int ddl_sec_context,
1862 int *ddl_save_nestlevel)
1863{
1864 ListCell *nextExclOp;
1865 ListCell *lc;
1866 int attn;
1867 int nkeycols = indexInfo->ii_NumIndexKeyAttrs;
1868 Oid save_userid;
1869 int save_sec_context;
1870
1871 /* Allocate space for exclusion operator info, if needed */
1872 if (exclusionOpNames)
1873 {
1874 Assert(list_length(exclusionOpNames) == nkeycols);
1875 indexInfo->ii_ExclusionOps = palloc_array(Oid, nkeycols);
1876 indexInfo->ii_ExclusionProcs = palloc_array(Oid, nkeycols);
1877 indexInfo->ii_ExclusionStrats = palloc_array(uint16, nkeycols);
1878 nextExclOp = list_head(exclusionOpNames);
1879 }
1880 else
1881 nextExclOp = NULL;
1882
1883 /*
1884 * If this is a WITHOUT OVERLAPS constraint, we need space for exclusion
1885 * ops, but we don't need to parse anything, so we can let nextExclOp be
1886 * NULL. Note that for partitions/inheriting/LIKE, exclusionOpNames will
1887 * be set, so we already allocated above.
1888 */
1889 if (iswithoutoverlaps)
1890 {
1891 if (exclusionOpNames == NIL)
1892 {
1893 indexInfo->ii_ExclusionOps = palloc_array(Oid, nkeycols);
1894 indexInfo->ii_ExclusionProcs = palloc_array(Oid, nkeycols);
1895 indexInfo->ii_ExclusionStrats = palloc_array(uint16, nkeycols);
1896 }
1897 nextExclOp = NULL;
1898 }
1899
1900 if (OidIsValid(ddl_userid))
1901 GetUserIdAndSecContext(&save_userid, &save_sec_context);
1902
1903 /*
1904 * process attributeList
1905 */
1906 attn = 0;
1907 foreach(lc, attList)
1908 {
1909 IndexElem *attribute = (IndexElem *) lfirst(lc);
1910 Oid atttype;
1911 Oid attcollation;
1912
1913 /*
1914 * Process the column-or-expression to be indexed.
1915 */
1916 if (attribute->name != NULL)
1917 {
1918 /* Simple index attribute */
1919 HeapTuple atttuple;
1920 Form_pg_attribute attform;
1921
1922 Assert(attribute->expr == NULL);
1923 atttuple = SearchSysCacheAttName(relId, attribute->name);
1924 if (!HeapTupleIsValid(atttuple))
1925 {
1926 /* difference in error message spellings is historical */
1927 if (isconstraint)
1928 ereport(ERROR,
1929 (errcode(ERRCODE_UNDEFINED_COLUMN),
1930 errmsg("column \"%s\" named in key does not exist",
1931 attribute->name)));
1932 else
1933 ereport(ERROR,
1934 (errcode(ERRCODE_UNDEFINED_COLUMN),
1935 errmsg("column \"%s\" does not exist",
1936 attribute->name)));
1937 }
1938 attform = (Form_pg_attribute) GETSTRUCT(atttuple);
1939 indexInfo->ii_IndexAttrNumbers[attn] = attform->attnum;
1940 atttype = attform->atttypid;
1941 attcollation = attform->attcollation;
1942 ReleaseSysCache(atttuple);
1943 }
1944 else
1945 {
1946 /* Index expression */
1947 Node *expr = attribute->expr;
1948
1949 Assert(expr != NULL);
1950
1951 if (attn >= nkeycols)
1952 ereport(ERROR,
1953 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1954 errmsg("expressions are not supported in included columns")));
1955 atttype = exprType(expr);
1956 attcollation = exprCollation(expr);
1957
1958 /*
1959 * Strip any top-level COLLATE clause. This ensures that we treat
1960 * "x COLLATE y" and "(x COLLATE y)" alike.
1961 */
1962 while (IsA(expr, CollateExpr))
1963 expr = (Node *) ((CollateExpr *) expr)->arg;
1964
1965 if (IsA(expr, Var) &&
1966 ((Var *) expr)->varattno != InvalidAttrNumber)
1967 {
1968 /*
1969 * User wrote "(column)" or "(column COLLATE something)".
1970 * Treat it like simple attribute anyway.
1971 */
1972 indexInfo->ii_IndexAttrNumbers[attn] = ((Var *) expr)->varattno;
1973 }
1974 else
1975 {
1976 indexInfo->ii_IndexAttrNumbers[attn] = 0; /* marks expression */
1977 indexInfo->ii_Expressions = lappend(indexInfo->ii_Expressions,
1978 expr);
1979
1980 /*
1981 * transformExpr() should have already rejected subqueries,
1982 * aggregates, and window functions, based on the EXPR_KIND_
1983 * for an index expression.
1984 */
1985
1986 /*
1987 * An expression using mutable functions is probably wrong,
1988 * since if you aren't going to get the same result for the
1989 * same data every time, it's not clear what the index entries
1990 * mean at all.
1991 */
1993 ereport(ERROR,
1994 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1995 errmsg("functions in index expression must be marked IMMUTABLE")));
1996 }
1997 }
1998
1999 typeOids[attn] = atttype;
2000
2001 /*
2002 * Included columns have no collation, no opclass and no ordering
2003 * options.
2004 */
2005 if (attn >= nkeycols)
2006 {
2007 if (attribute->collation)
2008 ereport(ERROR,
2009 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2010 errmsg("including column does not support a collation")));
2011 if (attribute->opclass)
2012 ereport(ERROR,
2013 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2014 errmsg("including column does not support an operator class")));
2015 if (attribute->ordering != SORTBY_DEFAULT)
2016 ereport(ERROR,
2017 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2018 errmsg("including column does not support ASC/DESC options")));
2019 if (attribute->nulls_ordering != SORTBY_NULLS_DEFAULT)
2020 ereport(ERROR,
2021 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2022 errmsg("including column does not support NULLS FIRST/LAST options")));
2023
2024 opclassOids[attn] = InvalidOid;
2025 opclassOptions[attn] = (Datum) 0;
2026 colOptions[attn] = 0;
2027 collationOids[attn] = InvalidOid;
2028 attn++;
2029
2030 continue;
2031 }
2032
2033 /*
2034 * Apply collation override if any. Use of ddl_userid is necessary
2035 * due to ACL checks therein, and it's safe because collations don't
2036 * contain opaque expressions (or non-opaque expressions).
2037 */
2038 if (attribute->collation)
2039 {
2040 if (OidIsValid(ddl_userid))
2041 {
2042 AtEOXact_GUC(false, *ddl_save_nestlevel);
2043 SetUserIdAndSecContext(ddl_userid, ddl_sec_context);
2044 }
2045 attcollation = get_collation_oid(attribute->collation, false);
2046 if (OidIsValid(ddl_userid))
2047 {
2048 SetUserIdAndSecContext(save_userid, save_sec_context);
2049 *ddl_save_nestlevel = NewGUCNestLevel();
2051 }
2052 }
2053
2054 /*
2055 * Check we have a collation iff it's a collatable type. The only
2056 * expected failures here are (1) COLLATE applied to a noncollatable
2057 * type, or (2) index expression had an unresolved collation. But we
2058 * might as well code this to be a complete consistency check.
2059 */
2060 if (type_is_collatable(atttype))
2061 {
2062 if (!OidIsValid(attcollation))
2063 ereport(ERROR,
2064 (errcode(ERRCODE_INDETERMINATE_COLLATION),
2065 errmsg("could not determine which collation to use for index expression"),
2066 errhint("Use the COLLATE clause to set the collation explicitly.")));
2067 }
2068 else
2069 {
2070 if (OidIsValid(attcollation))
2071 ereport(ERROR,
2072 (errcode(ERRCODE_DATATYPE_MISMATCH),
2073 errmsg("collations are not supported by type %s",
2074 format_type_be(atttype))));
2075 }
2076
2077 collationOids[attn] = attcollation;
2078
2079 /*
2080 * Identify the opclass to use. Use of ddl_userid is necessary due to
2081 * ACL checks therein. This is safe despite opclasses containing
2082 * opaque expressions (specifically, functions), because only
2083 * superusers can define opclasses.
2084 */
2085 if (OidIsValid(ddl_userid))
2086 {
2087 AtEOXact_GUC(false, *ddl_save_nestlevel);
2088 SetUserIdAndSecContext(ddl_userid, ddl_sec_context);
2089 }
2090 opclassOids[attn] = ResolveOpClass(attribute->opclass,
2091 atttype,
2092 accessMethodName,
2093 accessMethodId);
2094 if (OidIsValid(ddl_userid))
2095 {
2096 SetUserIdAndSecContext(save_userid, save_sec_context);
2097 *ddl_save_nestlevel = NewGUCNestLevel();
2099 }
2100
2101 /*
2102 * Identify the exclusion operator, if any.
2103 */
2104 if (nextExclOp)
2105 {
2106 List *opname = (List *) lfirst(nextExclOp);
2107 Oid opid;
2108 Oid opfamily;
2109 int strat;
2110
2111 /*
2112 * Find the operator --- it must accept the column datatype
2113 * without runtime coercion (but binary compatibility is OK).
2114 * Operators contain opaque expressions (specifically, functions).
2115 * compatible_oper_opid() boils down to oper() and
2116 * IsBinaryCoercible(). PostgreSQL would have security problems
2117 * elsewhere if oper() started calling opaque expressions.
2118 */
2119 if (OidIsValid(ddl_userid))
2120 {
2121 AtEOXact_GUC(false, *ddl_save_nestlevel);
2122 SetUserIdAndSecContext(ddl_userid, ddl_sec_context);
2123 }
2124 opid = compatible_oper_opid(opname, atttype, atttype, false);
2125 if (OidIsValid(ddl_userid))
2126 {
2127 SetUserIdAndSecContext(save_userid, save_sec_context);
2128 *ddl_save_nestlevel = NewGUCNestLevel();
2130 }
2131
2132 /*
2133 * Only allow commutative operators to be used in exclusion
2134 * constraints. If X conflicts with Y, but Y does not conflict
2135 * with X, bad things will happen.
2136 */
2137 if (get_commutator(opid) != opid)
2138 ereport(ERROR,
2139 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2140 errmsg("operator %s is not commutative",
2141 format_operator(opid)),
2142 errdetail("Only commutative operators can be used in exclusion constraints.")));
2143
2144 /*
2145 * Operator must be a member of the right opfamily, too
2146 */
2147 opfamily = get_opclass_family(opclassOids[attn]);
2148 strat = get_op_opfamily_strategy(opid, opfamily);
2149 if (strat == 0)
2150 {
2151 HeapTuple opftuple;
2152 Form_pg_opfamily opfform;
2153
2154 /*
2155 * attribute->opclass might not explicitly name the opfamily,
2156 * so fetch the name of the selected opfamily for use in the
2157 * error message.
2158 */
2159 opftuple = SearchSysCache1(OPFAMILYOID,
2160 ObjectIdGetDatum(opfamily));
2161 if (!HeapTupleIsValid(opftuple))
2162 elog(ERROR, "cache lookup failed for opfamily %u",
2163 opfamily);
2164 opfform = (Form_pg_opfamily) GETSTRUCT(opftuple);
2165
2166 ereport(ERROR,
2167 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2168 errmsg("operator %s is not a member of operator family \"%s\"",
2169 format_operator(opid),
2170 NameStr(opfform->opfname)),
2171 errdetail("The exclusion operator must be related to the index operator class for the constraint.")));
2172 }
2173
2174 indexInfo->ii_ExclusionOps[attn] = opid;
2175 indexInfo->ii_ExclusionProcs[attn] = get_opcode(opid);
2176 indexInfo->ii_ExclusionStrats[attn] = strat;
2177 nextExclOp = lnext(exclusionOpNames, nextExclOp);
2178 }
2179 else if (iswithoutoverlaps)
2180 {
2181 CompareType cmptype;
2182 StrategyNumber strat;
2183 Oid opid;
2184
2185 if (attn == nkeycols - 1)
2186 cmptype = COMPARE_OVERLAP;
2187 else
2188 cmptype = COMPARE_EQ;
2189 GetOperatorFromCompareType(opclassOids[attn], InvalidOid, cmptype, &opid, &strat);
2190 indexInfo->ii_ExclusionOps[attn] = opid;
2191 indexInfo->ii_ExclusionProcs[attn] = get_opcode(opid);
2192 indexInfo->ii_ExclusionStrats[attn] = strat;
2193 }
2194
2195 /*
2196 * Set up the per-column options (indoption field). For now, this is
2197 * zero for any un-ordered index, while ordered indexes have DESC and
2198 * NULLS FIRST/LAST options.
2199 */
2200 colOptions[attn] = 0;
2201 if (amcanorder)
2202 {
2203 /* default ordering is ASC */
2204 if (attribute->ordering == SORTBY_DESC)
2205 colOptions[attn] |= INDOPTION_DESC;
2206 /* default null ordering is LAST for ASC, FIRST for DESC */
2207 if (attribute->nulls_ordering == SORTBY_NULLS_DEFAULT)
2208 {
2209 if (attribute->ordering == SORTBY_DESC)
2210 colOptions[attn] |= INDOPTION_NULLS_FIRST;
2211 }
2212 else if (attribute->nulls_ordering == SORTBY_NULLS_FIRST)
2213 colOptions[attn] |= INDOPTION_NULLS_FIRST;
2214 }
2215 else
2216 {
2217 /* index AM does not support ordering */
2218 if (attribute->ordering != SORTBY_DEFAULT)
2219 ereport(ERROR,
2220 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2221 errmsg("access method \"%s\" does not support ASC/DESC options",
2222 accessMethodName)));
2223 if (attribute->nulls_ordering != SORTBY_NULLS_DEFAULT)
2224 ereport(ERROR,
2225 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2226 errmsg("access method \"%s\" does not support NULLS FIRST/LAST options",
2227 accessMethodName)));
2228 }
2229
2230 /* Set up the per-column opclass options (attoptions field). */
2231 if (attribute->opclassopts)
2232 {
2233 Assert(attn < nkeycols);
2234
2235 opclassOptions[attn] =
2236 transformRelOptions((Datum) 0, attribute->opclassopts,
2237 NULL, NULL, false, false);
2238 }
2239 else
2240 opclassOptions[attn] = (Datum) 0;
2241
2242 attn++;
2243 }
2244}
2245
2246/*
2247 * Resolve possibly-defaulted operator class specification
2248 *
2249 * Note: This is used to resolve operator class specifications in index and
2250 * partition key definitions.
2251 */
2252Oid
2253ResolveOpClass(const List *opclass, Oid attrType,
2254 const char *accessMethodName, Oid accessMethodId)
2255{
2256 char *schemaname;
2257 char *opcname;
2258 HeapTuple tuple;
2259 Form_pg_opclass opform;
2260 Oid opClassId,
2261 opInputType;
2262
2263 if (opclass == NIL)
2264 {
2265 /* no operator class specified, so find the default */
2266 opClassId = GetDefaultOpClass(attrType, accessMethodId);
2267 if (!OidIsValid(opClassId))
2268 ereport(ERROR,
2269 (errcode(ERRCODE_UNDEFINED_OBJECT),
2270 errmsg("data type %s has no default operator class for access method \"%s\"",
2271 format_type_be(attrType), accessMethodName),
2272 errhint("You must specify an operator class for the index or define a default operator class for the data type.")));
2273 return opClassId;
2274 }
2275
2276 /*
2277 * Specific opclass name given, so look up the opclass.
2278 */
2279
2280 /* deconstruct the name list */
2281 DeconstructQualifiedName(opclass, &schemaname, &opcname);
2282
2283 if (schemaname)
2284 {
2285 /* Look in specific schema only */
2286 Oid namespaceId;
2287
2288 namespaceId = LookupExplicitNamespace(schemaname, false);
2289 tuple = SearchSysCache3(CLAAMNAMENSP,
2290 ObjectIdGetDatum(accessMethodId),
2291 PointerGetDatum(opcname),
2292 ObjectIdGetDatum(namespaceId));
2293 }
2294 else
2295 {
2296 /* Unqualified opclass name, so search the search path */
2297 opClassId = OpclassnameGetOpcid(accessMethodId, opcname);
2298 if (!OidIsValid(opClassId))
2299 ereport(ERROR,
2300 (errcode(ERRCODE_UNDEFINED_OBJECT),
2301 errmsg("operator class \"%s\" does not exist for access method \"%s\"",
2302 opcname, accessMethodName)));
2303 tuple = SearchSysCache1(CLAOID, ObjectIdGetDatum(opClassId));
2304 }
2305
2306 if (!HeapTupleIsValid(tuple))
2307 ereport(ERROR,
2308 (errcode(ERRCODE_UNDEFINED_OBJECT),
2309 errmsg("operator class \"%s\" does not exist for access method \"%s\"",
2310 NameListToString(opclass), accessMethodName)));
2311
2312 /*
2313 * Verify that the index operator class accepts this datatype. Note we
2314 * will accept binary compatibility.
2315 */
2316 opform = (Form_pg_opclass) GETSTRUCT(tuple);
2317 opClassId = opform->oid;
2318 opInputType = opform->opcintype;
2319
2320 if (!IsBinaryCoercible(attrType, opInputType))
2321 ereport(ERROR,
2322 (errcode(ERRCODE_DATATYPE_MISMATCH),
2323 errmsg("operator class \"%s\" does not accept data type %s",
2324 NameListToString(opclass), format_type_be(attrType))));
2325
2326 ReleaseSysCache(tuple);
2327
2328 return opClassId;
2329}
2330
2331/*
2332 * GetDefaultOpClass
2333 *
2334 * Given the OIDs of a datatype and an access method, find the default
2335 * operator class, if any. Returns InvalidOid if there is none.
2336 */
2337Oid
2339{
2340 Oid result = InvalidOid;
2341 int nexact = 0;
2342 int ncompatible = 0;
2343 int ncompatiblepreferred = 0;
2344 Relation rel;
2345 ScanKeyData skey[1];
2346 SysScanDesc scan;
2347 HeapTuple tup;
2348 TYPCATEGORY tcategory;
2349
2350 /* If it's a domain, look at the base type instead */
2351 type_id = getBaseType(type_id);
2352
2353 tcategory = TypeCategory(type_id);
2354
2355 /*
2356 * We scan through all the opclasses available for the access method,
2357 * looking for one that is marked default and matches the target type
2358 * (either exactly or binary-compatibly, but prefer an exact match).
2359 *
2360 * We could find more than one binary-compatible match. If just one is
2361 * for a preferred type, use that one; otherwise we fail, forcing the user
2362 * to specify which one he wants. (The preferred-type special case is a
2363 * kluge for varchar: it's binary-compatible to both text and bpchar, so
2364 * we need a tiebreaker.) If we find more than one exact match, then
2365 * someone put bogus entries in pg_opclass.
2366 */
2367 rel = table_open(OperatorClassRelationId, AccessShareLock);
2368
2369 ScanKeyInit(&skey[0],
2370 Anum_pg_opclass_opcmethod,
2371 BTEqualStrategyNumber, F_OIDEQ,
2372 ObjectIdGetDatum(am_id));
2373
2374 scan = systable_beginscan(rel, OpclassAmNameNspIndexId, true,
2375 NULL, 1, skey);
2376
2377 while (HeapTupleIsValid(tup = systable_getnext(scan)))
2378 {
2379 Form_pg_opclass opclass = (Form_pg_opclass) GETSTRUCT(tup);
2380
2381 /* ignore altogether if not a default opclass */
2382 if (!opclass->opcdefault)
2383 continue;
2384 if (opclass->opcintype == type_id)
2385 {
2386 nexact++;
2387 result = opclass->oid;
2388 }
2389 else if (nexact == 0 &&
2390 IsBinaryCoercible(type_id, opclass->opcintype))
2391 {
2392 if (IsPreferredType(tcategory, opclass->opcintype))
2393 {
2394 ncompatiblepreferred++;
2395 result = opclass->oid;
2396 }
2397 else if (ncompatiblepreferred == 0)
2398 {
2399 ncompatible++;
2400 result = opclass->oid;
2401 }
2402 }
2403 }
2404
2405 systable_endscan(scan);
2406
2408
2409 /* raise error if pg_opclass contains inconsistent data */
2410 if (nexact > 1)
2411 ereport(ERROR,
2413 errmsg("there are multiple default operator classes for data type %s",
2414 format_type_be(type_id))));
2415
2416 if (nexact == 1 ||
2417 ncompatiblepreferred == 1 ||
2418 (ncompatiblepreferred == 0 && ncompatible == 1))
2419 return result;
2420
2421 return InvalidOid;
2422}
2423
2424/*
2425 * GetOperatorFromCompareType
2426 *
2427 * opclass - the opclass to use
2428 * rhstype - the type for the right-hand side, or InvalidOid to use the type of the given opclass.
2429 * cmptype - kind of operator to find
2430 * opid - holds the operator we found
2431 * strat - holds the output strategy number
2432 *
2433 * Finds an operator from a CompareType. This is used for temporal index
2434 * constraints (and other temporal features) to look up equality and overlaps
2435 * operators. We ask an opclass support function to translate from the
2436 * compare type to the internal strategy numbers. If the function isn't
2437 * defined or it gives no result, we set *strat to InvalidStrategy.
2438 */
2439void
2441 Oid *opid, StrategyNumber *strat)
2442{
2443 Oid opfamily;
2444 Oid opcintype;
2445
2446 Assert(cmptype == COMPARE_EQ || cmptype == COMPARE_OVERLAP || cmptype == COMPARE_CONTAINED_BY);
2447
2448 *opid = InvalidOid;
2449
2450 if (get_opclass_opfamily_and_input_type(opclass, &opfamily, &opcintype))
2451 {
2452 /*
2453 * Ask the opclass to translate to its internal stratnum
2454 *
2455 * For now we only need GiST support, but this could support other
2456 * indexams if we wanted.
2457 */
2458 *strat = GistTranslateStratnum(opclass, cmptype);
2459 if (*strat == InvalidStrategy)
2460 {
2461 HeapTuple tuple;
2462
2463 tuple = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclass));
2464 if (!HeapTupleIsValid(tuple))
2465 elog(ERROR, "cache lookup failed for operator class %u", opclass);
2466
2467 ereport(ERROR,
2468 errcode(ERRCODE_UNDEFINED_OBJECT),
2469 cmptype = COMPARE_EQ ? errmsg("could not identify an equality operator for type %s", format_type_be(opcintype)) :
2470 cmptype == COMPARE_OVERLAP ? errmsg("could not identify an overlaps operator for type %s", format_type_be(opcintype)) :
2471 cmptype == COMPARE_CONTAINED_BY ? errmsg("could not identify a contained-by operator for type %s", format_type_be(opcintype)) : 0,
2472 errdetail("Could not translate compare type %d for operator class \"%s\" for access method \"%s\".",
2473 cmptype, NameStr(((Form_pg_opclass) GETSTRUCT(tuple))->opcname), "gist"));
2474 }
2475
2476 /*
2477 * We parameterize rhstype so foreign keys can ask for a <@ operator
2478 * whose rhs matches the aggregate function. For example range_agg
2479 * returns anymultirange.
2480 */
2481 if (!OidIsValid(rhstype))
2482 rhstype = opcintype;
2483 *opid = get_opfamily_member(opfamily, opcintype, rhstype, *strat);
2484 }
2485
2486 if (!OidIsValid(*opid))
2487 {
2488 HeapTuple tuple;
2489
2490 tuple = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamily));
2491 if (!HeapTupleIsValid(tuple))
2492 elog(ERROR, "cache lookup failed for operator family %u", opfamily);
2493
2494 ereport(ERROR,
2495 errcode(ERRCODE_UNDEFINED_OBJECT),
2496 cmptype == COMPARE_EQ ? errmsg("could not identify an equality operator for type %s", format_type_be(opcintype)) :
2497 cmptype == COMPARE_OVERLAP ? errmsg("could not identify an overlaps operator for type %s", format_type_be(opcintype)) :
2498 cmptype == COMPARE_CONTAINED_BY ? errmsg("could not identify a contained-by operator for type %s", format_type_be(opcintype)) : 0,
2499 errdetail("There is no suitable operator in operator family \"%s\" for access method \"%s\".",
2500 NameStr(((Form_pg_opfamily) GETSTRUCT(tuple))->opfname), "gist"));
2501 }
2502}
2503
2504/*
2505 * makeObjectName()
2506 *
2507 * Create a name for an implicitly created index, sequence, constraint,
2508 * extended statistics, etc.
2509 *
2510 * The parameters are typically: the original table name, the original field
2511 * name, and a "type" string (such as "seq" or "pkey"). The field name
2512 * and/or type can be NULL if not relevant.
2513 *
2514 * The result is a palloc'd string.
2515 *
2516 * The basic result we want is "name1_name2_label", omitting "_name2" or
2517 * "_label" when those parameters are NULL. However, we must generate
2518 * a name with less than NAMEDATALEN characters! So, we truncate one or
2519 * both names if necessary to make a short-enough string. The label part
2520 * is never truncated (so it had better be reasonably short).
2521 *
2522 * The caller is responsible for checking uniqueness of the generated
2523 * name and retrying as needed; retrying will be done by altering the
2524 * "label" string (which is why we never truncate that part).
2525 */
2526char *
2527makeObjectName(const char *name1, const char *name2, const char *label)
2528{
2529 char *name;
2530 int overhead = 0; /* chars needed for label and underscores */
2531 int availchars; /* chars available for name(s) */
2532 int name1chars; /* chars allocated to name1 */
2533 int name2chars; /* chars allocated to name2 */
2534 int ndx;
2535
2536 name1chars = strlen(name1);
2537 if (name2)
2538 {
2539 name2chars = strlen(name2);
2540 overhead++; /* allow for separating underscore */
2541 }
2542 else
2543 name2chars = 0;
2544 if (label)
2545 overhead += strlen(label) + 1;
2546
2547 availchars = NAMEDATALEN - 1 - overhead;
2548 Assert(availchars > 0); /* else caller chose a bad label */
2549
2550 /*
2551 * If we must truncate, preferentially truncate the longer name. This
2552 * logic could be expressed without a loop, but it's simple and obvious as
2553 * a loop.
2554 */
2555 while (name1chars + name2chars > availchars)
2556 {
2557 if (name1chars > name2chars)
2558 name1chars--;
2559 else
2560 name2chars--;
2561 }
2562
2563 name1chars = pg_mbcliplen(name1, name1chars, name1chars);
2564 if (name2)
2565 name2chars = pg_mbcliplen(name2, name2chars, name2chars);
2566
2567 /* Now construct the string using the chosen lengths */
2568 name = palloc(name1chars + name2chars + overhead + 1);
2569 memcpy(name, name1, name1chars);
2570 ndx = name1chars;
2571 if (name2)
2572 {
2573 name[ndx++] = '_';
2574 memcpy(name + ndx, name2, name2chars);
2575 ndx += name2chars;
2576 }
2577 if (label)
2578 {
2579 name[ndx++] = '_';
2580 strcpy(name + ndx, label);
2581 }
2582 else
2583 name[ndx] = '\0';
2584
2585 return name;
2586}
2587
2588/*
2589 * Select a nonconflicting name for a new relation. This is ordinarily
2590 * used to choose index names (which is why it's here) but it can also
2591 * be used for sequences, or any autogenerated relation kind.
2592 *
2593 * name1, name2, and label are used the same way as for makeObjectName(),
2594 * except that the label can't be NULL; digits will be appended to the label
2595 * if needed to create a name that is unique within the specified namespace.
2596 *
2597 * If isconstraint is true, we also avoid choosing a name matching any
2598 * existing constraint in the same namespace. (This is stricter than what
2599 * Postgres itself requires, but the SQL standard says that constraint names
2600 * should be unique within schemas, so we follow that for autogenerated
2601 * constraint names.)
2602 *
2603 * Note: it is theoretically possible to get a collision anyway, if someone
2604 * else chooses the same name concurrently. This is fairly unlikely to be
2605 * a problem in practice, especially if one is holding an exclusive lock on
2606 * the relation identified by name1. However, if choosing multiple names
2607 * within a single command, you'd better create the new object and do
2608 * CommandCounterIncrement before choosing the next one!
2609 *
2610 * Returns a palloc'd string.
2611 */
2612char *
2613ChooseRelationName(const char *name1, const char *name2,
2614 const char *label, Oid namespaceid,
2615 bool isconstraint)
2616{
2617 int pass = 0;
2618 char *relname = NULL;
2619 char modlabel[NAMEDATALEN];
2620
2621 /* try the unmodified label first */
2622 strlcpy(modlabel, label, sizeof(modlabel));
2623
2624 for (;;)
2625 {
2626 relname = makeObjectName(name1, name2, modlabel);
2627
2628 if (!OidIsValid(get_relname_relid(relname, namespaceid)))
2629 {
2630 if (!isconstraint ||
2631 !ConstraintNameExists(relname, namespaceid))
2632 break;
2633 }
2634
2635 /* found a conflict, so try a new name component */
2636 pfree(relname);
2637 snprintf(modlabel, sizeof(modlabel), "%s%d", label, ++pass);
2638 }
2639
2640 return relname;
2641}
2642
2643/*
2644 * Select the name to be used for an index.
2645 *
2646 * The argument list is pretty ad-hoc :-(
2647 */
2648static char *
2649ChooseIndexName(const char *tabname, Oid namespaceId,
2650 const List *colnames, const List *exclusionOpNames,
2651 bool primary, bool isconstraint)
2652{
2653 char *indexname;
2654
2655 if (primary)
2656 {
2657 /* the primary key's name does not depend on the specific column(s) */
2658 indexname = ChooseRelationName(tabname,
2659 NULL,
2660 "pkey",
2661 namespaceId,
2662 true);
2663 }
2664 else if (exclusionOpNames != NIL)
2665 {
2666 indexname = ChooseRelationName(tabname,
2667 ChooseIndexNameAddition(colnames),
2668 "excl",
2669 namespaceId,
2670 true);
2671 }
2672 else if (isconstraint)
2673 {
2674 indexname = ChooseRelationName(tabname,
2675 ChooseIndexNameAddition(colnames),
2676 "key",
2677 namespaceId,
2678 true);
2679 }
2680 else
2681 {
2682 indexname = ChooseRelationName(tabname,
2683 ChooseIndexNameAddition(colnames),
2684 "idx",
2685 namespaceId,
2686 false);
2687 }
2688
2689 return indexname;
2690}
2691
2692/*
2693 * Generate "name2" for a new index given the list of column names for it
2694 * (as produced by ChooseIndexColumnNames). This will be passed to
2695 * ChooseRelationName along with the parent table name and a suitable label.
2696 *
2697 * We know that less than NAMEDATALEN characters will actually be used,
2698 * so we can truncate the result once we've generated that many.
2699 *
2700 * XXX See also ChooseForeignKeyConstraintNameAddition and
2701 * ChooseExtendedStatisticNameAddition.
2702 */
2703static char *
2705{
2706 char buf[NAMEDATALEN * 2];
2707 int buflen = 0;
2708 ListCell *lc;
2709
2710 buf[0] = '\0';
2711 foreach(lc, colnames)
2712 {
2713 const char *name = (const char *) lfirst(lc);
2714
2715 if (buflen > 0)
2716 buf[buflen++] = '_'; /* insert _ between names */
2717
2718 /*
2719 * At this point we have buflen <= NAMEDATALEN. name should be less
2720 * than NAMEDATALEN already, but use strlcpy for paranoia.
2721 */
2722 strlcpy(buf + buflen, name, NAMEDATALEN);
2723 buflen += strlen(buf + buflen);
2724 if (buflen >= NAMEDATALEN)
2725 break;
2726 }
2727 return pstrdup(buf);
2728}
2729
2730/*
2731 * Select the actual names to be used for the columns of an index, given the
2732 * list of IndexElems for the columns. This is mostly about ensuring the
2733 * names are unique so we don't get a conflicting-attribute-names error.
2734 *
2735 * Returns a List of plain strings (char *, not String nodes).
2736 */
2737static List *
2739{
2740 List *result = NIL;
2741 ListCell *lc;
2742
2743 foreach(lc, indexElems)
2744 {
2745 IndexElem *ielem = (IndexElem *) lfirst(lc);
2746 const char *origname;
2747 const char *curname;
2748 int i;
2749 char buf[NAMEDATALEN];
2750
2751 /* Get the preliminary name from the IndexElem */
2752 if (ielem->indexcolname)
2753 origname = ielem->indexcolname; /* caller-specified name */
2754 else if (ielem->name)
2755 origname = ielem->name; /* simple column reference */
2756 else
2757 origname = "expr"; /* default name for expression */
2758
2759 /* If it conflicts with any previous column, tweak it */
2760 curname = origname;
2761 for (i = 1;; i++)
2762 {
2763 ListCell *lc2;
2764 char nbuf[32];
2765 int nlen;
2766
2767 foreach(lc2, result)
2768 {
2769 if (strcmp(curname, (char *) lfirst(lc2)) == 0)
2770 break;
2771 }
2772 if (lc2 == NULL)
2773 break; /* found nonconflicting name */
2774
2775 sprintf(nbuf, "%d", i);
2776
2777 /* Ensure generated names are shorter than NAMEDATALEN */
2778 nlen = pg_mbcliplen(origname, strlen(origname),
2779 NAMEDATALEN - 1 - strlen(nbuf));
2780 memcpy(buf, origname, nlen);
2781 strcpy(buf + nlen, nbuf);
2782 curname = buf;
2783 }
2784
2785 /* And attach to the result list */
2786 result = lappend(result, pstrdup(curname));
2787 }
2788 return result;
2789}
2790
2791/*
2792 * ExecReindex
2793 *
2794 * Primary entry point for manual REINDEX commands. This is mainly a
2795 * preparation wrapper for the real operations that will happen in
2796 * each subroutine of REINDEX.
2797 */
2798void
2799ExecReindex(ParseState *pstate, const ReindexStmt *stmt, bool isTopLevel)
2800{
2801 ReindexParams params = {0};
2802 ListCell *lc;
2803 bool concurrently = false;
2804 bool verbose = false;
2805 char *tablespacename = NULL;
2806
2807 /* Parse option list */
2808 foreach(lc, stmt->params)
2809 {
2810 DefElem *opt = (DefElem *) lfirst(lc);
2811
2812 if (strcmp(opt->defname, "verbose") == 0)
2813 verbose = defGetBoolean(opt);
2814 else if (strcmp(opt->defname, "concurrently") == 0)
2815 concurrently = defGetBoolean(opt);
2816 else if (strcmp(opt->defname, "tablespace") == 0)
2817 tablespacename = defGetString(opt);
2818 else
2819 ereport(ERROR,
2820 (errcode(ERRCODE_SYNTAX_ERROR),
2821 errmsg("unrecognized REINDEX option \"%s\"",
2822 opt->defname),
2823 parser_errposition(pstate, opt->location)));
2824 }
2825
2826 if (concurrently)
2827 PreventInTransactionBlock(isTopLevel,
2828 "REINDEX CONCURRENTLY");
2829
2830 params.options =
2831 (verbose ? REINDEXOPT_VERBOSE : 0) |
2832 (concurrently ? REINDEXOPT_CONCURRENTLY : 0);
2833
2834 /*
2835 * Assign the tablespace OID to move indexes to, with InvalidOid to do
2836 * nothing.
2837 */
2838 if (tablespacename != NULL)
2839 {
2840 params.tablespaceOid = get_tablespace_oid(tablespacename, false);
2841
2842 /* Check permissions except when moving to database's default */
2843 if (OidIsValid(params.tablespaceOid) &&
2845 {
2846 AclResult aclresult;
2847
2848 aclresult = object_aclcheck(TableSpaceRelationId, params.tablespaceOid,
2850 if (aclresult != ACLCHECK_OK)
2853 }
2854 }
2855 else
2856 params.tablespaceOid = InvalidOid;
2857
2858 switch (stmt->kind)
2859 {
2861 ReindexIndex(stmt, &params, isTopLevel);
2862 break;
2864 ReindexTable(stmt, &params, isTopLevel);
2865 break;
2869
2870 /*
2871 * This cannot run inside a user transaction block; if we were
2872 * inside a transaction, then its commit- and
2873 * start-transaction-command calls would not have the intended
2874 * effect!
2875 */
2876 PreventInTransactionBlock(isTopLevel,
2877 (stmt->kind == REINDEX_OBJECT_SCHEMA) ? "REINDEX SCHEMA" :
2878 (stmt->kind == REINDEX_OBJECT_SYSTEM) ? "REINDEX SYSTEM" :
2879 "REINDEX DATABASE");
2880 ReindexMultipleTables(stmt, &params);
2881 break;
2882 default:
2883 elog(ERROR, "unrecognized object type: %d",
2884 (int) stmt->kind);
2885 break;
2886 }
2887}
2888
2889/*
2890 * ReindexIndex
2891 * Recreate a specific index.
2892 */
2893static void
2894ReindexIndex(const ReindexStmt *stmt, const ReindexParams *params, bool isTopLevel)
2895{
2896 const RangeVar *indexRelation = stmt->relation;
2898 Oid indOid;
2899 char persistence;
2900 char relkind;
2901
2902 /*
2903 * Find and lock index, and check permissions on table; use callback to
2904 * obtain lock on table first, to avoid deadlock hazard. The lock level
2905 * used here must match the index lock obtained in reindex_index().
2906 *
2907 * If it's a temporary index, we will perform a non-concurrent reindex,
2908 * even if CONCURRENTLY was requested. In that case, reindex_index() will
2909 * upgrade the lock, but that's OK, because other sessions can't hold
2910 * locks on our temporary table.
2911 */
2912 state.params = *params;
2913 state.locked_table_oid = InvalidOid;
2914 indOid = RangeVarGetRelidExtended(indexRelation,
2917 0,
2919 &state);
2920
2921 /*
2922 * Obtain the current persistence and kind of the existing index. We
2923 * already hold a lock on the index.
2924 */
2925 persistence = get_rel_persistence(indOid);
2926 relkind = get_rel_relkind(indOid);
2927
2928 if (relkind == RELKIND_PARTITIONED_INDEX)
2929 ReindexPartitions(stmt, indOid, params, isTopLevel);
2930 else if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
2931 persistence != RELPERSISTENCE_TEMP)
2933 else
2934 {
2935 ReindexParams newparams = *params;
2936
2938 reindex_index(stmt, indOid, false, persistence, &newparams);
2939 }
2940}
2941
2942/*
2943 * Check permissions on table before acquiring relation lock; also lock
2944 * the heap before the RangeVarGetRelidExtended takes the index lock, to avoid
2945 * deadlocks.
2946 */
2947static void
2949 Oid relId, Oid oldRelId, void *arg)
2950{
2951 char relkind;
2953 LOCKMODE table_lockmode;
2954 Oid table_oid;
2955
2956 /*
2957 * Lock level here should match table lock in reindex_index() for
2958 * non-concurrent case and table locks used by index_concurrently_*() for
2959 * concurrent case.
2960 */
2961 table_lockmode = (state->params.options & REINDEXOPT_CONCURRENTLY) != 0 ?
2963
2964 /*
2965 * If we previously locked some other index's heap, and the name we're
2966 * looking up no longer refers to that relation, release the now-useless
2967 * lock.
2968 */
2969 if (relId != oldRelId && OidIsValid(oldRelId))
2970 {
2971 UnlockRelationOid(state->locked_table_oid, table_lockmode);
2972 state->locked_table_oid = InvalidOid;
2973 }
2974
2975 /* If the relation does not exist, there's nothing more to do. */
2976 if (!OidIsValid(relId))
2977 return;
2978
2979 /*
2980 * If the relation does exist, check whether it's an index. But note that
2981 * the relation might have been dropped between the time we did the name
2982 * lookup and now. In that case, there's nothing to do.
2983 */
2984 relkind = get_rel_relkind(relId);
2985 if (!relkind)
2986 return;
2987 if (relkind != RELKIND_INDEX &&
2988 relkind != RELKIND_PARTITIONED_INDEX)
2989 ereport(ERROR,
2990 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2991 errmsg("\"%s\" is not an index", relation->relname)));
2992
2993 /* Check permissions */
2994 table_oid = IndexGetRelation(relId, true);
2995 if (OidIsValid(table_oid))
2996 {
2997 AclResult aclresult;
2998
2999 aclresult = pg_class_aclcheck(table_oid, GetUserId(), ACL_MAINTAIN);
3000 if (aclresult != ACLCHECK_OK)
3001 aclcheck_error(aclresult, OBJECT_INDEX, relation->relname);
3002 }
3003
3004 /* Lock heap before index to avoid deadlock. */
3005 if (relId != oldRelId)
3006 {
3007 /*
3008 * If the OID isn't valid, it means the index was concurrently
3009 * dropped, which is not a problem for us; just return normally.
3010 */
3011 if (OidIsValid(table_oid))
3012 {
3013 LockRelationOid(table_oid, table_lockmode);
3014 state->locked_table_oid = table_oid;
3015 }
3016 }
3017}
3018
3019/*
3020 * ReindexTable
3021 * Recreate all indexes of a table (and of its toast table, if any)
3022 */
3023static Oid
3024ReindexTable(const ReindexStmt *stmt, const ReindexParams *params, bool isTopLevel)
3025{
3026 Oid heapOid;
3027 bool result;
3028 const RangeVar *relation = stmt->relation;
3029
3030 /*
3031 * The lock level used here should match reindex_relation().
3032 *
3033 * If it's a temporary table, we will perform a non-concurrent reindex,
3034 * even if CONCURRENTLY was requested. In that case, reindex_relation()
3035 * will upgrade the lock, but that's OK, because other sessions can't hold
3036 * locks on our temporary table.
3037 */
3038 heapOid = RangeVarGetRelidExtended(relation,
3041 0,
3043
3044 if (get_rel_relkind(heapOid) == RELKIND_PARTITIONED_TABLE)
3045 ReindexPartitions(stmt, heapOid, params, isTopLevel);
3046 else if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
3047 get_rel_persistence(heapOid) != RELPERSISTENCE_TEMP)
3048 {
3049 result = ReindexRelationConcurrently(stmt, heapOid, params);
3050
3051 if (!result)
3053 (errmsg("table \"%s\" has no indexes that can be reindexed concurrently",
3054 relation->relname)));
3055 }
3056 else
3057 {
3058 ReindexParams newparams = *params;
3059
3061 result = reindex_relation(stmt, heapOid,
3064 &newparams);
3065 if (!result)
3067 (errmsg("table \"%s\" has no indexes to reindex",
3068 relation->relname)));
3069 }
3070
3071 return heapOid;
3072}
3073
3074/*
3075 * ReindexMultipleTables
3076 * Recreate indexes of tables selected by objectName/objectKind.
3077 *
3078 * To reduce the probability of deadlocks, each table is reindexed in a
3079 * separate transaction, so we can release the lock on it right away.
3080 * That means this must not be called within a user transaction block!
3081 */
3082static void
3084{
3085
3086 Oid objectOid;
3087 Relation relationRelation;
3088 TableScanDesc scan;
3089 ScanKeyData scan_keys[1];
3090 HeapTuple tuple;
3091 MemoryContext private_context;
3092 MemoryContext old;
3093 List *relids = NIL;
3094 int num_keys;
3095 bool concurrent_warning = false;
3096 bool tablespace_warning = false;
3097 const char *objectName = stmt->name;
3098 const ReindexObjectType objectKind = stmt->kind;
3099
3100 Assert(objectKind == REINDEX_OBJECT_SCHEMA ||
3101 objectKind == REINDEX_OBJECT_SYSTEM ||
3102 objectKind == REINDEX_OBJECT_DATABASE);
3103
3104 /*
3105 * This matches the options enforced by the grammar, where the object name
3106 * is optional for DATABASE and SYSTEM.
3107 */
3108 Assert(objectName || objectKind != REINDEX_OBJECT_SCHEMA);
3109
3110 if (objectKind == REINDEX_OBJECT_SYSTEM &&
3112 ereport(ERROR,
3113 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3114 errmsg("cannot reindex system catalogs concurrently")));
3115
3116 /*
3117 * Get OID of object to reindex, being the database currently being used
3118 * by session for a database or for system catalogs, or the schema defined
3119 * by caller. At the same time do permission checks that need different
3120 * processing depending on the object type.
3121 */
3122 if (objectKind == REINDEX_OBJECT_SCHEMA)
3123 {
3124 objectOid = get_namespace_oid(objectName, false);
3125
3126 if (!object_ownercheck(NamespaceRelationId, objectOid, GetUserId()) &&
3127 !has_privs_of_role(GetUserId(), ROLE_PG_MAINTAIN))
3129 objectName);
3130 }
3131 else
3132 {
3133 objectOid = MyDatabaseId;
3134
3135 if (objectName && strcmp(objectName, get_database_name(objectOid)) != 0)
3136 ereport(ERROR,
3137 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3138 errmsg("can only reindex the currently open database")));
3139 if (!object_ownercheck(DatabaseRelationId, objectOid, GetUserId()) &&
3140 !has_privs_of_role(GetUserId(), ROLE_PG_MAINTAIN))
3142 get_database_name(objectOid));
3143 }
3144
3145 /*
3146 * Create a memory context that will survive forced transaction commits we
3147 * do below. Since it is a child of PortalContext, it will go away
3148 * eventually even if we suffer an error; there's no need for special
3149 * abort cleanup logic.
3150 */
3151 private_context = AllocSetContextCreate(PortalContext,
3152 "ReindexMultipleTables",
3154
3155 /*
3156 * Define the search keys to find the objects to reindex. For a schema, we
3157 * select target relations using relnamespace, something not necessary for
3158 * a database-wide operation.
3159 */
3160 if (objectKind == REINDEX_OBJECT_SCHEMA)
3161 {
3162 num_keys = 1;
3163 ScanKeyInit(&scan_keys[0],
3164 Anum_pg_class_relnamespace,
3165 BTEqualStrategyNumber, F_OIDEQ,
3166 ObjectIdGetDatum(objectOid));
3167 }
3168 else
3169 num_keys = 0;
3170
3171 /*
3172 * Scan pg_class to build a list of the relations we need to reindex.
3173 *
3174 * We only consider plain relations and materialized views here (toast
3175 * rels will be processed indirectly by reindex_relation).
3176 */
3177 relationRelation = table_open(RelationRelationId, AccessShareLock);
3178 scan = table_beginscan_catalog(relationRelation, num_keys, scan_keys);
3179 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
3180 {
3181 Form_pg_class classtuple = (Form_pg_class) GETSTRUCT(tuple);
3182 Oid relid = classtuple->oid;
3183
3184 /*
3185 * Only regular tables and matviews can have indexes, so ignore any
3186 * other kind of relation.
3187 *
3188 * Partitioned tables/indexes are skipped but matching leaf partitions
3189 * are processed.
3190 */
3191 if (classtuple->relkind != RELKIND_RELATION &&
3192 classtuple->relkind != RELKIND_MATVIEW)
3193 continue;
3194
3195 /* Skip temp tables of other backends; we can't reindex them at all */
3196 if (classtuple->relpersistence == RELPERSISTENCE_TEMP &&
3197 !isTempNamespace(classtuple->relnamespace))
3198 continue;
3199
3200 /*
3201 * Check user/system classification. SYSTEM processes all the
3202 * catalogs, and DATABASE processes everything that's not a catalog.
3203 */
3204 if (objectKind == REINDEX_OBJECT_SYSTEM &&
3205 !IsCatalogRelationOid(relid))
3206 continue;
3207 else if (objectKind == REINDEX_OBJECT_DATABASE &&
3208 IsCatalogRelationOid(relid))
3209 continue;
3210
3211 /*
3212 * We already checked privileges on the database or schema, but we
3213 * further restrict reindexing shared catalogs to roles with the
3214 * MAINTAIN privilege on the relation.
3215 */
3216 if (classtuple->relisshared &&
3218 continue;
3219
3220 /*
3221 * Skip system tables, since index_create() would reject indexing them
3222 * concurrently (and it would likely fail if we tried).
3223 */
3224 if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
3225 IsCatalogRelationOid(relid))
3226 {
3227 if (!concurrent_warning)
3229 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3230 errmsg("cannot reindex system catalogs concurrently, skipping all")));
3231 concurrent_warning = true;
3232 continue;
3233 }
3234
3235 /*
3236 * If a new tablespace is set, check if this relation has to be
3237 * skipped.
3238 */
3240 {
3241 bool skip_rel = false;
3242
3243 /*
3244 * Mapped relations cannot be moved to different tablespaces (in
3245 * particular this eliminates all shared catalogs.).
3246 */
3247 if (RELKIND_HAS_STORAGE(classtuple->relkind) &&
3248 !RelFileNumberIsValid(classtuple->relfilenode))
3249 skip_rel = true;
3250
3251 /*
3252 * A system relation is always skipped, even with
3253 * allow_system_table_mods enabled.
3254 */
3255 if (IsSystemClass(relid, classtuple))
3256 skip_rel = true;
3257
3258 if (skip_rel)
3259 {
3260 if (!tablespace_warning)
3262 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3263 errmsg("cannot move system relations, skipping all")));
3264 tablespace_warning = true;
3265 continue;
3266 }
3267 }
3268
3269 /* Save the list of relation OIDs in private context */
3270 old = MemoryContextSwitchTo(private_context);
3271
3272 /*
3273 * We always want to reindex pg_class first if it's selected to be
3274 * reindexed. This ensures that if there is any corruption in
3275 * pg_class' indexes, they will be fixed before we process any other
3276 * tables. This is critical because reindexing itself will try to
3277 * update pg_class.
3278 */
3279 if (relid == RelationRelationId)
3280 relids = lcons_oid(relid, relids);
3281 else
3282 relids = lappend_oid(relids, relid);
3283
3285 }
3286 table_endscan(scan);
3287 table_close(relationRelation, AccessShareLock);
3288
3289 /*
3290 * Process each relation listed in a separate transaction. Note that this
3291 * commits and then starts a new transaction immediately.
3292 */
3294
3295 MemoryContextDelete(private_context);
3296}
3297
3298/*
3299 * Error callback specific to ReindexPartitions().
3300 */
3301static void
3303{
3304 ReindexErrorInfo *errinfo = (ReindexErrorInfo *) arg;
3305
3306 Assert(RELKIND_HAS_PARTITIONS(errinfo->relkind));
3307
3308 if (errinfo->relkind == RELKIND_PARTITIONED_TABLE)
3309 errcontext("while reindexing partitioned table \"%s.%s\"",
3310 errinfo->relnamespace, errinfo->relname);
3311 else if (errinfo->relkind == RELKIND_PARTITIONED_INDEX)
3312 errcontext("while reindexing partitioned index \"%s.%s\"",
3313 errinfo->relnamespace, errinfo->relname);
3314}
3315
3316/*
3317 * ReindexPartitions
3318 *
3319 * Reindex a set of partitions, per the partitioned index or table given
3320 * by the caller.
3321 */
3322static void
3323ReindexPartitions(const ReindexStmt *stmt, Oid relid, const ReindexParams *params, bool isTopLevel)
3324{
3325 List *partitions = NIL;
3326 char relkind = get_rel_relkind(relid);
3327 char *relname = get_rel_name(relid);
3328 char *relnamespace = get_namespace_name(get_rel_namespace(relid));
3329 MemoryContext reindex_context;
3330 List *inhoids;
3331 ListCell *lc;
3332 ErrorContextCallback errcallback;
3333 ReindexErrorInfo errinfo;
3334
3335 Assert(RELKIND_HAS_PARTITIONS(relkind));
3336
3337 /*
3338 * Check if this runs in a transaction block, with an error callback to
3339 * provide more context under which a problem happens.
3340 */
3341 errinfo.relname = pstrdup(relname);
3342 errinfo.relnamespace = pstrdup(relnamespace);
3343 errinfo.relkind = relkind;
3344 errcallback.callback = reindex_error_callback;
3345 errcallback.arg = &errinfo;
3346 errcallback.previous = error_context_stack;
3347 error_context_stack = &errcallback;
3348
3349 PreventInTransactionBlock(isTopLevel,
3350 relkind == RELKIND_PARTITIONED_TABLE ?
3351 "REINDEX TABLE" : "REINDEX INDEX");
3352
3353 /* Pop the error context stack */
3354 error_context_stack = errcallback.previous;
3355
3356 /*
3357 * Create special memory context for cross-transaction storage.
3358 *
3359 * Since it is a child of PortalContext, it will go away eventually even
3360 * if we suffer an error so there is no need for special abort cleanup
3361 * logic.
3362 */
3363 reindex_context = AllocSetContextCreate(PortalContext, "Reindex",
3365
3366 /* ShareLock is enough to prevent schema modifications */
3367 inhoids = find_all_inheritors(relid, ShareLock, NULL);
3368
3369 /*
3370 * The list of relations to reindex are the physical partitions of the
3371 * tree so discard any partitioned table or index.
3372 */
3373 foreach(lc, inhoids)
3374 {
3375 Oid partoid = lfirst_oid(lc);
3376 char partkind = get_rel_relkind(partoid);
3377 MemoryContext old_context;
3378
3379 /*
3380 * This discards partitioned tables, partitioned indexes and foreign
3381 * tables.
3382 */
3383 if (!RELKIND_HAS_STORAGE(partkind))
3384 continue;
3385
3386 Assert(partkind == RELKIND_INDEX ||
3387 partkind == RELKIND_RELATION);
3388
3389 /* Save partition OID */
3390 old_context = MemoryContextSwitchTo(reindex_context);
3391 partitions = lappend_oid(partitions, partoid);
3392 MemoryContextSwitchTo(old_context);
3393 }
3394
3395 /*
3396 * Process each partition listed in a separate transaction. Note that
3397 * this commits and then starts a new transaction immediately.
3398 */
3400
3401 /*
3402 * Clean up working storage --- note we must do this after
3403 * StartTransactionCommand, else we might be trying to delete the active
3404 * context!
3405 */
3406 MemoryContextDelete(reindex_context);
3407}
3408
3409/*
3410 * ReindexMultipleInternal
3411 *
3412 * Reindex a list of relations, each one being processed in its own
3413 * transaction. This commits the existing transaction immediately,
3414 * and starts a new transaction when finished.
3415 */
3416static void
3418{
3419 ListCell *l;
3420
3423
3424 foreach(l, relids)
3425 {
3426 Oid relid = lfirst_oid(l);
3427 char relkind;
3428 char relpersistence;
3429
3431
3432 /* functions in indexes may want a snapshot set */
3434
3435 /* check if the relation still exists */
3436 if (!SearchSysCacheExists1(RELOID, ObjectIdGetDatum(relid)))
3437 {
3440 continue;
3441 }
3442
3443 /*
3444 * Check permissions except when moving to database's default if a new
3445 * tablespace is chosen. Note that this check also happens in
3446 * ExecReindex(), but we do an extra check here as this runs across
3447 * multiple transactions.
3448 */
3451 {
3452 AclResult aclresult;
3453
3454 aclresult = object_aclcheck(TableSpaceRelationId, params->tablespaceOid,
3456 if (aclresult != ACLCHECK_OK)
3459 }
3460
3461 relkind = get_rel_relkind(relid);
3462 relpersistence = get_rel_persistence(relid);
3463
3464 /*
3465 * Partitioned tables and indexes can never be processed directly, and
3466 * a list of their leaves should be built first.
3467 */
3468 Assert(!RELKIND_HAS_PARTITIONS(relkind));
3469
3470 if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
3471 relpersistence != RELPERSISTENCE_TEMP)
3472 {
3473 ReindexParams newparams = *params;
3474
3475 newparams.options |= REINDEXOPT_MISSING_OK;
3476 (void) ReindexRelationConcurrently(stmt, relid, &newparams);
3477 if (ActiveSnapshotSet())
3479 /* ReindexRelationConcurrently() does the verbose output */
3480 }
3481 else if (relkind == RELKIND_INDEX)
3482 {
3483 ReindexParams newparams = *params;
3484
3485 newparams.options |=
3487 reindex_index(stmt, relid, false, relpersistence, &newparams);
3489 /* reindex_index() does the verbose output */
3490 }
3491 else
3492 {
3493 bool result;
3494 ReindexParams newparams = *params;
3495
3496 newparams.options |=
3498 result = reindex_relation(stmt, relid,
3501 &newparams);
3502
3503 if (result && (params->options & REINDEXOPT_VERBOSE) != 0)
3504 ereport(INFO,
3505 (errmsg("table \"%s.%s\" was reindexed",
3507 get_rel_name(relid))));
3508
3510 }
3511
3513 }
3514
3516}
3517
3518
3519/*
3520 * ReindexRelationConcurrently - process REINDEX CONCURRENTLY for given
3521 * relation OID
3522 *
3523 * 'relationOid' can either belong to an index, a table or a materialized
3524 * view. For tables and materialized views, all its indexes will be rebuilt,
3525 * excluding invalid indexes and any indexes used in exclusion constraints,
3526 * but including its associated toast table indexes. For indexes, the index
3527 * itself will be rebuilt.
3528 *
3529 * The locks taken on parent tables and involved indexes are kept until the
3530 * transaction is committed, at which point a session lock is taken on each
3531 * relation. Both of these protect against concurrent schema changes.
3532 *
3533 * Returns true if any indexes have been rebuilt (including toast table's
3534 * indexes, when relevant), otherwise returns false.
3535 *
3536 * NOTE: This cannot be used on temporary relations. A concurrent build would
3537 * cause issues with ON COMMIT actions triggered by the transactions of the
3538 * concurrent build. Temporary relations are not subject to concurrent
3539 * concerns, so there's no need for the more complicated concurrent build,
3540 * anyway, and a non-concurrent reindex is more efficient.
3541 */
3542static bool
3544{
3545 typedef struct ReindexIndexInfo
3546 {
3547 Oid indexId;
3548 Oid tableId;
3549 Oid amId;
3550 bool safe; /* for set_indexsafe_procflags */
3551 } ReindexIndexInfo;
3552 List *heapRelationIds = NIL;
3553 List *indexIds = NIL;
3554 List *newIndexIds = NIL;
3555 List *relationLocks = NIL;
3556 List *lockTags = NIL;
3557 ListCell *lc,
3558 *lc2;
3559 MemoryContext private_context;
3560 MemoryContext oldcontext;
3561 char relkind;
3562 char *relationName = NULL;
3563 char *relationNamespace = NULL;
3564 PGRUsage ru0;
3565 const int progress_index[] = {
3570 };
3571 int64 progress_vals[4];
3572
3573 /*
3574 * Create a memory context that will survive forced transaction commits we
3575 * do below. Since it is a child of PortalContext, it will go away
3576 * eventually even if we suffer an error; there's no need for special
3577 * abort cleanup logic.
3578 */
3579 private_context = AllocSetContextCreate(PortalContext,
3580 "ReindexConcurrent",
3582
3583 if ((params->options & REINDEXOPT_VERBOSE) != 0)
3584 {
3585 /* Save data needed by REINDEX VERBOSE in private context */
3586 oldcontext = MemoryContextSwitchTo(private_context);
3587
3588 relationName = get_rel_name(relationOid);
3589 relationNamespace = get_namespace_name(get_rel_namespace(relationOid));
3590
3591 pg_rusage_init(&ru0);
3592
3593 MemoryContextSwitchTo(oldcontext);
3594 }
3595
3596 relkind = get_rel_relkind(relationOid);
3597
3598 /*
3599 * Extract the list of indexes that are going to be rebuilt based on the
3600 * relation Oid given by caller.
3601 */
3602 switch (relkind)
3603 {
3604 case RELKIND_RELATION:
3605 case RELKIND_MATVIEW:
3606 case RELKIND_TOASTVALUE:
3607 {
3608 /*
3609 * In the case of a relation, find all its indexes including
3610 * toast indexes.
3611 */
3612 Relation heapRelation;
3613
3614 /* Save the list of relation OIDs in private context */
3615 oldcontext = MemoryContextSwitchTo(private_context);
3616
3617 /* Track this relation for session locks */
3618 heapRelationIds = lappend_oid(heapRelationIds, relationOid);
3619
3620 MemoryContextSwitchTo(oldcontext);
3621
3622 if (IsCatalogRelationOid(relationOid))
3623 ereport(ERROR,
3624 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3625 errmsg("cannot reindex system catalogs concurrently")));
3626
3627 /* Open relation to get its indexes */
3628 if ((params->options & REINDEXOPT_MISSING_OK) != 0)
3629 {
3630 heapRelation = try_table_open(relationOid,
3632 /* leave if relation does not exist */
3633 if (!heapRelation)
3634 break;
3635 }
3636 else
3637 heapRelation = table_open(relationOid,
3639
3640 if (OidIsValid(params->tablespaceOid) &&
3641 IsSystemRelation(heapRelation))
3642 ereport(ERROR,
3643 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3644 errmsg("cannot move system relation \"%s\"",
3645 RelationGetRelationName(heapRelation))));
3646
3647 /* Add all the valid indexes of relation to list */
3648 foreach(lc, RelationGetIndexList(heapRelation))
3649 {
3650 Oid cellOid = lfirst_oid(lc);
3651 Relation indexRelation = index_open(cellOid,
3653
3654 if (!indexRelation->rd_index->indisvalid)
3656 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3657 errmsg("skipping reindex of invalid index \"%s.%s\"",
3659 get_rel_name(cellOid)),
3660 errhint("Use DROP INDEX or REINDEX INDEX.")));
3661 else if (indexRelation->rd_index->indisexclusion)
3663 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3664 errmsg("cannot reindex exclusion constraint index \"%s.%s\" concurrently, skipping",
3666 get_rel_name(cellOid))));
3667 else
3668 {
3669 ReindexIndexInfo *idx;
3670
3671 /* Save the list of relation OIDs in private context */
3672 oldcontext = MemoryContextSwitchTo(private_context);
3673
3674 idx = palloc_object(ReindexIndexInfo);
3675 idx->indexId = cellOid;
3676 /* other fields set later */
3677
3678 indexIds = lappend(indexIds, idx);
3679
3680 MemoryContextSwitchTo(oldcontext);
3681 }
3682
3683 index_close(indexRelation, NoLock);
3684 }
3685
3686 /* Also add the toast indexes */
3687 if (OidIsValid(heapRelation->rd_rel->reltoastrelid))
3688 {
3689 Oid toastOid = heapRelation->rd_rel->reltoastrelid;
3690 Relation toastRelation = table_open(toastOid,
3692
3693 /* Save the list of relation OIDs in private context */
3694 oldcontext = MemoryContextSwitchTo(private_context);
3695
3696 /* Track this relation for session locks */
3697 heapRelationIds = lappend_oid(heapRelationIds, toastOid);
3698
3699 MemoryContextSwitchTo(oldcontext);
3700
3701 foreach(lc2, RelationGetIndexList(toastRelation))
3702 {
3703 Oid cellOid = lfirst_oid(lc2);
3704 Relation indexRelation = index_open(cellOid,
3706
3707 if (!indexRelation->rd_index->indisvalid)
3709 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3710 errmsg("skipping reindex of invalid index \"%s.%s\"",
3712 get_rel_name(cellOid)),
3713 errhint("Use DROP INDEX or REINDEX INDEX.")));
3714 else
3715 {
3716 ReindexIndexInfo *idx;
3717
3718 /*
3719 * Save the list of relation OIDs in private
3720 * context
3721 */
3722 oldcontext = MemoryContextSwitchTo(private_context);
3723
3724 idx = palloc_object(ReindexIndexInfo);
3725 idx->indexId = cellOid;
3726 indexIds = lappend(indexIds, idx);
3727 /* other fields set later */
3728
3729 MemoryContextSwitchTo(oldcontext);
3730 }
3731
3732 index_close(indexRelation, NoLock);
3733 }
3734
3735 table_close(toastRelation, NoLock);
3736 }
3737
3738 table_close(heapRelation, NoLock);
3739 break;
3740 }
3741 case RELKIND_INDEX:
3742 {
3743 Oid heapId = IndexGetRelation(relationOid,
3744 (params->options & REINDEXOPT_MISSING_OK) != 0);
3745 Relation heapRelation;
3746 ReindexIndexInfo *idx;
3747
3748 /* if relation is missing, leave */
3749 if (!OidIsValid(heapId))
3750 break;
3751
3752 if (IsCatalogRelationOid(heapId))
3753 ereport(ERROR,
3754 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3755 errmsg("cannot reindex system catalogs concurrently")));
3756
3757 /*
3758 * Don't allow reindex for an invalid index on TOAST table, as
3759 * if rebuilt it would not be possible to drop it. Match
3760 * error message in reindex_index().
3761 */
3762 if (IsToastNamespace(get_rel_namespace(relationOid)) &&
3763 !get_index_isvalid(relationOid))
3764 ereport(ERROR,
3765 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3766 errmsg("cannot reindex invalid index on TOAST table")));
3767
3768 /*
3769 * Check if parent relation can be locked and if it exists,
3770 * this needs to be done at this stage as the list of indexes
3771 * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
3772 * should not be used once all the session locks are taken.
3773 */
3774 if ((params->options & REINDEXOPT_MISSING_OK) != 0)
3775 {
3776 heapRelation = try_table_open(heapId,
3778 /* leave if relation does not exist */
3779 if (!heapRelation)
3780 break;
3781 }
3782 else
3783 heapRelation = table_open(heapId,
3785
3786 if (OidIsValid(params->tablespaceOid) &&
3787 IsSystemRelation(heapRelation))
3788 ereport(ERROR,
3789 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3790 errmsg("cannot move system relation \"%s\"",
3791 get_rel_name(relationOid))));
3792
3793 table_close(heapRelation, NoLock);
3794
3795 /* Save the list of relation OIDs in private context */
3796 oldcontext = MemoryContextSwitchTo(private_context);
3797
3798 /* Track the heap relation of this index for session locks */
3799 heapRelationIds = list_make1_oid(heapId);
3800
3801 /*
3802 * Save the list of relation OIDs in private context. Note
3803 * that invalid indexes are allowed here.
3804 */
3805 idx = palloc_object(ReindexIndexInfo);
3806 idx->indexId = relationOid;
3807 indexIds = lappend(indexIds, idx);
3808 /* other fields set later */
3809
3810 MemoryContextSwitchTo(oldcontext);
3811 break;
3812 }
3813
3814 case RELKIND_PARTITIONED_TABLE:
3815 case RELKIND_PARTITIONED_INDEX:
3816 default:
3817 /* Return error if type of relation is not supported */
3818 ereport(ERROR,
3819 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3820 errmsg("cannot reindex this type of relation concurrently")));
3821 break;
3822 }
3823
3824 /*
3825 * Definitely no indexes, so leave. Any checks based on
3826 * REINDEXOPT_MISSING_OK should be done only while the list of indexes to
3827 * work on is built as the session locks taken before this transaction
3828 * commits will make sure that they cannot be dropped by a concurrent
3829 * session until this operation completes.
3830 */
3831 if (indexIds == NIL)
3832 return false;
3833
3834 /* It's not a shared catalog, so refuse to move it to shared tablespace */
3835 if (params->tablespaceOid == GLOBALTABLESPACE_OID)
3836 ereport(ERROR,
3837 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3838 errmsg("cannot move non-shared relation to tablespace \"%s\"",
3840
3841 Assert(heapRelationIds != NIL);
3842
3843 /*-----
3844 * Now we have all the indexes we want to process in indexIds.
3845 *
3846 * The phases now are:
3847 *
3848 * 1. create new indexes in the catalog
3849 * 2. build new indexes
3850 * 3. let new indexes catch up with tuples inserted in the meantime
3851 * 4. swap index names
3852 * 5. mark old indexes as dead
3853 * 6. drop old indexes
3854 *
3855 * We process each phase for all indexes before moving to the next phase,
3856 * for efficiency.
3857 */
3858
3859 /*
3860 * Phase 1 of REINDEX CONCURRENTLY
3861 *
3862 * Create a new index with the same properties as the old one, but it is
3863 * only registered in catalogs and will be built later. Then get session
3864 * locks on all involved tables. See analogous code in DefineIndex() for
3865 * more detailed comments.
3866 */
3867
3868 foreach(lc, indexIds)
3869 {
3870 char *concurrentName;
3871 ReindexIndexInfo *idx = lfirst(lc);
3872 ReindexIndexInfo *newidx;
3873 Oid newIndexId;
3874 Relation indexRel;
3875 Relation heapRel;
3876 Oid save_userid;
3877 int save_sec_context;
3878 int save_nestlevel;
3879 Relation newIndexRel;
3880 LockRelId *lockrelid;
3881 Oid tablespaceid;
3882
3883 indexRel = index_open(idx->indexId, ShareUpdateExclusiveLock);
3884 heapRel = table_open(indexRel->rd_index->indrelid,
3886
3887 /*
3888 * Switch to the table owner's userid, so that any index functions are
3889 * run as that user. Also lock down security-restricted operations
3890 * and arrange to make GUC variable changes local to this command.
3891 */
3892 GetUserIdAndSecContext(&save_userid, &save_sec_context);
3893 SetUserIdAndSecContext(heapRel->rd_rel->relowner,
3894 save_sec_context | SECURITY_RESTRICTED_OPERATION);
3895 save_nestlevel = NewGUCNestLevel();
3897
3898 /* determine safety of this index for set_indexsafe_procflags */
3899 idx->safe = (RelationGetIndexExpressions(indexRel) == NIL &&
3900 RelationGetIndexPredicate(indexRel) == NIL);
3901
3902#ifdef USE_INJECTION_POINTS
3903 if (idx->safe)
3904 INJECTION_POINT("reindex-conc-index-safe");
3905 else
3906 INJECTION_POINT("reindex-conc-index-not-safe");
3907#endif
3908
3909 idx->tableId = RelationGetRelid(heapRel);
3910 idx->amId = indexRel->rd_rel->relam;
3911
3912 /* This function shouldn't be called for temporary relations. */
3913 if (indexRel->rd_rel->relpersistence == RELPERSISTENCE_TEMP)
3914 elog(ERROR, "cannot reindex a temporary table concurrently");
3915
3917
3919 progress_vals[1] = 0; /* initializing */
3920 progress_vals[2] = idx->indexId;
3921 progress_vals[3] = idx->amId;
3922 pgstat_progress_update_multi_param(4, progress_index, progress_vals);
3923
3924 /* Choose a temporary relation name for the new index */
3925 concurrentName = ChooseRelationName(get_rel_name(idx->indexId),
3926 NULL,
3927 "ccnew",
3928 get_rel_namespace(indexRel->rd_index->indrelid),
3929 false);
3930
3931 /* Choose the new tablespace, indexes of toast tables are not moved */
3932 if (OidIsValid(params->tablespaceOid) &&
3933 heapRel->rd_rel->relkind != RELKIND_TOASTVALUE)
3934 tablespaceid = params->tablespaceOid;
3935 else
3936 tablespaceid = indexRel->rd_rel->reltablespace;
3937
3938 /* Create new index definition based on given index */
3939 newIndexId = index_concurrently_create_copy(heapRel,
3940 idx->indexId,
3941 tablespaceid,
3942 concurrentName);
3943
3944 /*
3945 * Now open the relation of the new index, a session-level lock is
3946 * also needed on it.
3947 */
3948 newIndexRel = index_open(newIndexId, ShareUpdateExclusiveLock);
3949
3950 /*
3951 * Save the list of OIDs and locks in private context
3952 */
3953 oldcontext = MemoryContextSwitchTo(private_context);
3954
3955 newidx = palloc_object(ReindexIndexInfo);
3956 newidx->indexId = newIndexId;
3957 newidx->safe = idx->safe;
3958 newidx->tableId = idx->tableId;
3959 newidx->amId = idx->amId;
3960
3961 newIndexIds = lappend(newIndexIds, newidx);
3962
3963 /*
3964 * Save lockrelid to protect each relation from drop then close
3965 * relations. The lockrelid on parent relation is not taken here to
3966 * avoid multiple locks taken on the same relation, instead we rely on
3967 * parentRelationIds built earlier.
3968 */
3969 lockrelid = palloc_object(LockRelId);
3970 *lockrelid = indexRel->rd_lockInfo.lockRelId;
3971 relationLocks = lappend(relationLocks, lockrelid);
3972 lockrelid = palloc_object(LockRelId);
3973 *lockrelid = newIndexRel->rd_lockInfo.lockRelId;
3974 relationLocks = lappend(relationLocks, lockrelid);
3975
3976 MemoryContextSwitchTo(oldcontext);
3977
3978 index_close(indexRel, NoLock);
3979 index_close(newIndexRel, NoLock);
3980
3981 /* Roll back any GUC changes executed by index functions */
3982 AtEOXact_GUC(false, save_nestlevel);
3983
3984 /* Restore userid and security context */
3985 SetUserIdAndSecContext(save_userid, save_sec_context);
3986
3987 table_close(heapRel, NoLock);
3988
3989 /*
3990 * If a statement is available, telling that this comes from a REINDEX
3991 * command, collect the new index for event triggers.
3992 */
3993 if (stmt)
3994 {
3995 ObjectAddress address;
3996
3997 ObjectAddressSet(address, RelationRelationId, newIndexId);
4000 (Node *) stmt);
4001 }
4002 }
4003
4004 /*
4005 * Save the heap lock for following visibility checks with other backends
4006 * might conflict with this session.
4007 */
4008 foreach(lc, heapRelationIds)
4009 {
4011 LockRelId *lockrelid;
4012 LOCKTAG *heaplocktag;
4013
4014 /* Save the list of locks in private context */
4015 oldcontext = MemoryContextSwitchTo(private_context);
4016
4017 /* Add lockrelid of heap relation to the list of locked relations */
4018 lockrelid = palloc_object(LockRelId);
4019 *lockrelid = heapRelation->rd_lockInfo.lockRelId;
4020 relationLocks = lappend(relationLocks, lockrelid);
4021
4022 heaplocktag = palloc_object(LOCKTAG);
4023
4024 /* Save the LOCKTAG for this parent relation for the wait phase */
4025 SET_LOCKTAG_RELATION(*heaplocktag, lockrelid->dbId, lockrelid->relId);
4026 lockTags = lappend(lockTags, heaplocktag);
4027
4028 MemoryContextSwitchTo(oldcontext);
4029
4030 /* Close heap relation */
4031 table_close(heapRelation, NoLock);
4032 }
4033
4034 /* Get a session-level lock on each table. */
4035 foreach(lc, relationLocks)
4036 {
4037 LockRelId *lockrelid = (LockRelId *) lfirst(lc);
4038
4040 }
4041
4045
4046 /*
4047 * Because we don't take a snapshot in this transaction, there's no need
4048 * to set the PROC_IN_SAFE_IC flag here.
4049 */
4050
4051 /*
4052 * Phase 2 of REINDEX CONCURRENTLY
4053 *
4054 * Build the new indexes in a separate transaction for each index to avoid
4055 * having open transactions for an unnecessary long time. But before
4056 * doing that, wait until no running transactions could have the table of
4057 * the index open with the old list of indexes. See "phase 2" in
4058 * DefineIndex() for more details.
4059 */
4060
4063 WaitForLockersMultiple(lockTags, ShareLock, true);
4065
4066 foreach(lc, newIndexIds)
4067 {
4068 ReindexIndexInfo *newidx = lfirst(lc);
4069
4070 /* Start new transaction for this index's concurrent build */
4072
4073 /*
4074 * Check for user-requested abort. This is inside a transaction so as
4075 * xact.c does not issue a useless WARNING, and ensures that
4076 * session-level locks are cleaned up on abort.
4077 */
4079
4080 /* Tell concurrent indexing to ignore us, if index qualifies */
4081 if (newidx->safe)
4083
4084 /* Set ActiveSnapshot since functions in the indexes may need it */
4086
4087 /*
4088 * Update progress for the index to build, with the correct parent
4089 * table involved.
4090 */
4093 progress_vals[1] = PROGRESS_CREATEIDX_PHASE_BUILD;
4094 progress_vals[2] = newidx->indexId;
4095 progress_vals[3] = newidx->amId;
4096 pgstat_progress_update_multi_param(4, progress_index, progress_vals);
4097
4098 /* Perform concurrent build of new index */
4099 index_concurrently_build(newidx->tableId, newidx->indexId);
4100
4103 }
4104
4106
4107 /*
4108 * Because we don't take a snapshot or Xid in this transaction, there's no
4109 * need to set the PROC_IN_SAFE_IC flag here.
4110 */
4111
4112 /*
4113 * Phase 3 of REINDEX CONCURRENTLY
4114 *
4115 * During this phase the old indexes catch up with any new tuples that
4116 * were created during the previous phase. See "phase 3" in DefineIndex()
4117 * for more details.
4118 */
4119
4122 WaitForLockersMultiple(lockTags, ShareLock, true);
4124
4125 foreach(lc, newIndexIds)
4126 {
4127 ReindexIndexInfo *newidx = lfirst(lc);
4128 TransactionId limitXmin;
4129 Snapshot snapshot;
4130
4132
4133 /*
4134 * Check for user-requested abort. This is inside a transaction so as
4135 * xact.c does not issue a useless WARNING, and ensures that
4136 * session-level locks are cleaned up on abort.
4137 */
4139
4140 /* Tell concurrent indexing to ignore us, if index qualifies */
4141 if (newidx->safe)
4143
4144 /*
4145 * Take the "reference snapshot" that will be used by validate_index()
4146 * to filter candidate tuples.
4147 */
4149 PushActiveSnapshot(snapshot);
4150
4151 /*
4152 * Update progress for the index to build, with the correct parent
4153 * table involved.
4154 */
4158 progress_vals[2] = newidx->indexId;
4159 progress_vals[3] = newidx->amId;
4160 pgstat_progress_update_multi_param(4, progress_index, progress_vals);
4161
4162 validate_index(newidx->tableId, newidx->indexId, snapshot);
4163
4164 /*
4165 * We can now do away with our active snapshot, we still need to save
4166 * the xmin limit to wait for older snapshots.
4167 */
4168 limitXmin = snapshot->xmin;
4169
4171 UnregisterSnapshot(snapshot);
4172
4173 /*
4174 * To ensure no deadlocks, we must commit and start yet another
4175 * transaction, and do our wait before any snapshot has been taken in
4176 * it.
4177 */
4180
4181 /*
4182 * The index is now valid in the sense that it contains all currently
4183 * interesting tuples. But since it might not contain tuples deleted
4184 * just before the reference snap was taken, we have to wait out any
4185 * transactions that might have older snapshots.
4186 *
4187 * Because we don't take a snapshot or Xid in this transaction,
4188 * there's no need to set the PROC_IN_SAFE_IC flag here.
4189 */
4192 WaitForOlderSnapshots(limitXmin, true);
4193
4195 }
4196
4197 /*
4198 * Phase 4 of REINDEX CONCURRENTLY
4199 *
4200 * Now that the new indexes have been validated, swap each new index with
4201 * its corresponding old index.
4202 *
4203 * We mark the new indexes as valid and the old indexes as not valid at
4204 * the same time to make sure we only get constraint violations from the
4205 * indexes with the correct names.
4206 */
4207
4209
4210 /*
4211 * Because this transaction only does catalog manipulations and doesn't do
4212 * any index operations, we can set the PROC_IN_SAFE_IC flag here
4213 * unconditionally.
4214 */
4216
4217 forboth(lc, indexIds, lc2, newIndexIds)
4218 {
4219 ReindexIndexInfo *oldidx = lfirst(lc);
4220 ReindexIndexInfo *newidx = lfirst(lc2);
4221 char *oldName;
4222
4223 /*
4224 * Check for user-requested abort. This is inside a transaction so as
4225 * xact.c does not issue a useless WARNING, and ensures that
4226 * session-level locks are cleaned up on abort.
4227 */
4229
4230 /* Choose a relation name for old index */
4231 oldName = ChooseRelationName(get_rel_name(oldidx->indexId),
4232 NULL,
4233 "ccold",
4234 get_rel_namespace(oldidx->tableId),
4235 false);
4236
4237 /*
4238 * Updating pg_index might involve TOAST table access, so ensure we
4239 * have a valid snapshot.
4240 */
4242
4243 /*
4244 * Swap old index with the new one. This also marks the new one as
4245 * valid and the old one as not valid.
4246 */
4247 index_concurrently_swap(newidx->indexId, oldidx->indexId, oldName);
4248
4250
4251 /*
4252 * Invalidate the relcache for the table, so that after this commit
4253 * all sessions will refresh any cached plans that might reference the
4254 * index.
4255 */
4256 CacheInvalidateRelcacheByRelid(oldidx->tableId);
4257
4258 /*
4259 * CCI here so that subsequent iterations see the oldName in the
4260 * catalog and can choose a nonconflicting name for their oldName.
4261 * Otherwise, this could lead to conflicts if a table has two indexes
4262 * whose names are equal for the first NAMEDATALEN-minus-a-few
4263 * characters.
4264 */
4266 }
4267
4268 /* Commit this transaction and make index swaps visible */
4271
4272 /*
4273 * While we could set PROC_IN_SAFE_IC if all indexes qualified, there's no
4274 * real need for that, because we only acquire an Xid after the wait is
4275 * done, and that lasts for a very short period.
4276 */
4277
4278 /*
4279 * Phase 5 of REINDEX CONCURRENTLY
4280 *
4281 * Mark the old indexes as dead. First we must wait until no running
4282 * transaction could be using the index for a query. See also
4283 * index_drop() for more details.
4284 */
4285
4289
4290 foreach(lc, indexIds)
4291 {
4292 ReindexIndexInfo *oldidx = lfirst(lc);
4293
4294 /*
4295 * Check for user-requested abort. This is inside a transaction so as
4296 * xact.c does not issue a useless WARNING, and ensures that
4297 * session-level locks are cleaned up on abort.
4298 */
4300
4301 /*
4302 * Updating pg_index might involve TOAST table access, so ensure we
4303 * have a valid snapshot.
4304 */
4306
4307 index_concurrently_set_dead(oldidx->tableId, oldidx->indexId);
4308
4310 }
4311
4312 /* Commit this transaction to make the updates visible. */
4315
4316 /*
4317 * While we could set PROC_IN_SAFE_IC if all indexes qualified, there's no
4318 * real need for that, because we only acquire an Xid after the wait is
4319 * done, and that lasts for a very short period.
4320 */
4321
4322 /*
4323 * Phase 6 of REINDEX CONCURRENTLY
4324 *
4325 * Drop the old indexes.
4326 */
4327
4331
4333
4334 {
4336
4337 foreach(lc, indexIds)
4338 {
4339 ReindexIndexInfo *idx = lfirst(lc);
4340 ObjectAddress object;
4341
4342 object.classId = RelationRelationId;
4343 object.objectId = idx->indexId;
4344 object.objectSubId = 0;
4345
4346 add_exact_object_address(&object, objects);
4347 }
4348
4349 /*
4350 * Use PERFORM_DELETION_CONCURRENT_LOCK so that index_drop() uses the
4351 * right lock level.
4352 */
4355 }
4356
4359
4360 /*
4361 * Finally, release the session-level lock on the table.
4362 */
4363 foreach(lc, relationLocks)
4364 {
4365 LockRelId *lockrelid = (LockRelId *) lfirst(lc);
4366
4368 }
4369
4370 /* Start a new transaction to finish process properly */
4372
4373 /* Log what we did */
4374 if ((params->options & REINDEXOPT_VERBOSE) != 0)
4375 {
4376 if (relkind == RELKIND_INDEX)
4377 ereport(INFO,
4378 (errmsg("index \"%s.%s\" was reindexed",
4379 relationNamespace, relationName),
4380 errdetail("%s.",
4381 pg_rusage_show(&ru0))));
4382 else
4383 {
4384 foreach(lc, newIndexIds)
4385 {
4386 ReindexIndexInfo *idx = lfirst(lc);
4387 Oid indOid = idx->indexId;
4388
4389 ereport(INFO,
4390 (errmsg("index \"%s.%s\" was reindexed",
4392 get_rel_name(indOid))));
4393 /* Don't show rusage here, since it's not per index. */
4394 }
4395
4396 ereport(INFO,
4397 (errmsg("table \"%s.%s\" was reindexed",
4398 relationNamespace, relationName),
4399 errdetail("%s.",
4400 pg_rusage_show(&ru0))));
4401 }
4402 }
4403
4404 MemoryContextDelete(private_context);
4405
4407
4408 return true;
4409}
4410
4411/*
4412 * Insert or delete an appropriate pg_inherits tuple to make the given index
4413 * be a partition of the indicated parent index.
4414 *
4415 * This also corrects the pg_depend information for the affected index.
4416 */
4417void
4418IndexSetParentIndex(Relation partitionIdx, Oid parentOid)
4419{
4420 Relation pg_inherits;
4421 ScanKeyData key[2];
4422 SysScanDesc scan;
4423 Oid partRelid = RelationGetRelid(partitionIdx);
4424 HeapTuple tuple;
4425 bool fix_dependencies;
4426
4427 /* Make sure this is an index */
4428 Assert(partitionIdx->rd_rel->relkind == RELKIND_INDEX ||
4429 partitionIdx->rd_rel->relkind == RELKIND_PARTITIONED_INDEX);
4430
4431 /*
4432 * Scan pg_inherits for rows linking our index to some parent.
4433 */
4434 pg_inherits = relation_open(InheritsRelationId, RowExclusiveLock);
4435 ScanKeyInit(&key[0],
4436 Anum_pg_inherits_inhrelid,
4437 BTEqualStrategyNumber, F_OIDEQ,
4438 ObjectIdGetDatum(partRelid));
4439 ScanKeyInit(&key[1],
4440 Anum_pg_inherits_inhseqno,
4441 BTEqualStrategyNumber, F_INT4EQ,
4442 Int32GetDatum(1));
4443 scan = systable_beginscan(pg_inherits, InheritsRelidSeqnoIndexId, true,
4444 NULL, 2, key);
4445 tuple = systable_getnext(scan);
4446
4447 if (!HeapTupleIsValid(tuple))
4448 {
4449 if (parentOid == InvalidOid)
4450 {
4451 /*
4452 * No pg_inherits row, and no parent wanted: nothing to do in this
4453 * case.
4454 */
4455 fix_dependencies = false;
4456 }
4457 else
4458 {
4459 StoreSingleInheritance(partRelid, parentOid, 1);
4460 fix_dependencies = true;
4461 }
4462 }
4463 else
4464 {
4465 Form_pg_inherits inhForm = (Form_pg_inherits) GETSTRUCT(tuple);
4466
4467 if (parentOid == InvalidOid)
4468 {
4469 /*
4470 * There exists a pg_inherits row, which we want to clear; do so.
4471 */
4472 CatalogTupleDelete(pg_inherits, &tuple->t_self);
4473 fix_dependencies = true;
4474 }
4475 else
4476 {
4477 /*
4478 * A pg_inherits row exists. If it's the same we want, then we're
4479 * good; if it differs, that amounts to a corrupt catalog and
4480 * should not happen.
4481 */
4482 if (inhForm->inhparent != parentOid)
4483 {
4484 /* unexpected: we should not get called in this case */
4485 elog(ERROR, "bogus pg_inherit row: inhrelid %u inhparent %u",
4486 inhForm->inhrelid, inhForm->inhparent);
4487 }
4488
4489 /* already in the right state */
4490 fix_dependencies = false;
4491 }
4492 }
4493
4494 /* done with pg_inherits */
4495 systable_endscan(scan);
4496 relation_close(pg_inherits, RowExclusiveLock);
4497
4498 /* set relhassubclass if an index partition has been added to the parent */
4499 if (OidIsValid(parentOid))
4500 {
4502 SetRelationHasSubclass(parentOid, true);
4503 }
4504
4505 /* set relispartition correctly on the partition */
4506 update_relispartition(partRelid, OidIsValid(parentOid));
4507
4508 if (fix_dependencies)
4509 {
4510 /*
4511 * Insert/delete pg_depend rows. If setting a parent, add PARTITION
4512 * dependencies on the parent index and the table; if removing a
4513 * parent, delete PARTITION dependencies.
4514 */
4515 if (OidIsValid(parentOid))
4516 {
4517 ObjectAddress partIdx;
4518 ObjectAddress parentIdx;
4519 ObjectAddress partitionTbl;
4520
4521 ObjectAddressSet(partIdx, RelationRelationId, partRelid);
4522 ObjectAddressSet(parentIdx, RelationRelationId, parentOid);
4523 ObjectAddressSet(partitionTbl, RelationRelationId,
4524 partitionIdx->rd_index->indrelid);
4525 recordDependencyOn(&partIdx, &parentIdx,
4527 recordDependencyOn(&partIdx, &partitionTbl,
4529 }
4530 else
4531 {
4532 deleteDependencyRecordsForClass(RelationRelationId, partRelid,
4533 RelationRelationId,
4535 deleteDependencyRecordsForClass(RelationRelationId, partRelid,
4536 RelationRelationId,
4538 }
4539
4540 /* make our updates visible */
4542 }
4543}
4544
4545/*
4546 * Subroutine of IndexSetParentIndex to update the relispartition flag of the
4547 * given index to the given value.
4548 */
4549static void
4551{
4552 HeapTuple tup;
4553 Relation classRel;
4554 ItemPointerData otid;
4555
4556 classRel = table_open(RelationRelationId, RowExclusiveLock);
4557 tup = SearchSysCacheLockedCopy1(RELOID, ObjectIdGetDatum(relationId));
4558 if (!HeapTupleIsValid(tup))
4559 elog(ERROR, "cache lookup failed for relation %u", relationId);
4560 otid = tup->t_self;
4561 Assert(((Form_pg_class) GETSTRUCT(tup))->relispartition != newval);
4562 ((Form_pg_class) GETSTRUCT(tup))->relispartition = newval;
4563 CatalogTupleUpdate(classRel, &otid, tup);
4564 UnlockTuple(classRel, &otid, InplaceUpdateTupleLock);
4565 heap_freetuple(tup);
4566 table_close(classRel, RowExclusiveLock);
4567}
4568
4569/*
4570 * Set the PROC_IN_SAFE_IC flag in MyProc->statusFlags.
4571 *
4572 * When doing concurrent index builds, we can set this flag
4573 * to tell other processes concurrently running CREATE
4574 * INDEX CONCURRENTLY or REINDEX CONCURRENTLY to ignore us when
4575 * doing their waits for concurrent snapshots. On one hand it
4576 * avoids pointlessly waiting for a process that's not interesting
4577 * anyway; but more importantly it avoids deadlocks in some cases.
4578 *
4579 * This can be done safely only for indexes that don't execute any
4580 * expressions that could access other tables, so index must not be
4581 * expressional nor partial. Caller is responsible for only calling
4582 * this routine when that assumption holds true.
4583 *
4584 * (The flag is reset automatically at transaction end, so it must be
4585 * set for each transaction.)
4586 */
4587static inline void
4589{
4590 /*
4591 * This should only be called before installing xid or xmin in MyProc;
4592 * otherwise, concurrent processes could see an Xmin that moves backwards.
4593 */
4596
4597 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
4600 LWLockRelease(ProcArrayLock);
4601}
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:5268
AclResult
Definition: acl.h:182
@ ACLCHECK_OK
Definition: acl.h:183
@ ACLCHECK_NOT_OWNER
Definition: acl.h:185
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:2622
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
Definition: aclchk.c:3804
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
Definition: aclchk.c:4058
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4007
IndexAmRoutine * GetIndexAmRoutine(Oid amhandler)
Definition: amapi.c:33
bytea *(* amoptions_function)(Datum reloptions, bool validate)
Definition: amapi.h:155
void free_attrmap(AttrMap *map)
Definition: attmap.c:56
AttrMap * build_attrmap_by_name(TupleDesc indesc, TupleDesc outdesc, bool missing_ok)
Definition: attmap.c:177
int16 AttrNumber
Definition: attnum.h:21
#define InvalidAttrNumber
Definition: attnum.h:23
char * get_tablespace_name(Oid spc_oid)
Definition: tablespace.c:1472
Oid get_tablespace_oid(const char *tablespacename, bool missing_ok)
Definition: tablespace.c:1426
Oid GetDefaultTablespace(char relpersistence, bool partitioned)
Definition: tablespace.c:1143
void pgstat_progress_start_command(ProgressCommandType cmdtype, Oid relid)
void pgstat_progress_incr_param(int index, int64 incr)
void pgstat_progress_update_param(int index, int64 val)
void pgstat_progress_update_multi_param(int nparam, const int *index, const int64 *val)
void pgstat_progress_end_command(void)
@ PROGRESS_COMMAND_CREATE_INDEX
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
#define NameStr(name)
Definition: c.h:703
uint16 bits16
Definition: c.h:496
#define Assert(condition)
Definition: c.h:815
int64_t int64
Definition: c.h:485
int16_t int16
Definition: c.h:483
uint16_t uint16
Definition: c.h:487
uint32 TransactionId
Definition: c.h:609
#define OidIsValid(objectId)
Definition: c.h:732
bool IsToastNamespace(Oid namespaceId)
Definition: catalog.c:230
bool IsSystemRelation(Relation relation)
Definition: catalog.c:73
bool IsCatalogRelationOid(Oid relid)
Definition: catalog.c:120
bool IsSystemClass(Oid relid, Form_pg_class reltuple)
Definition: catalog.c:85
bool contain_mutable_functions_after_planning(Expr *expr)
Definition: clauses.c:489
void CreateComments(Oid oid, Oid classoid, int32 subid, const char *comment)
Definition: comment.c:143
char * get_database_name(Oid dbid)
Definition: dbcommands.c:3187
char * defGetString(DefElem *def)
Definition: define.c:35
bool defGetBoolean(DefElem *def)
Definition: define.c:94
void performMultipleDeletions(const ObjectAddresses *objects, DropBehavior behavior, int flags)
Definition: dependency.c:332
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
Definition: dependency.c:2548
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2502
@ DEPENDENCY_PARTITION_PRI
Definition: dependency.h:36
@ DEPENDENCY_PARTITION_SEC
Definition: dependency.h:37
#define PERFORM_DELETION_CONCURRENT_LOCK
Definition: dependency.h:97
#define PERFORM_DELETION_INTERNAL
Definition: dependency.h:92
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1157
int errdetail(const char *fmt,...)
Definition: elog.c:1203
ErrorContextCallback * error_context_stack
Definition: elog.c:94
int errhint(const char *fmt,...)
Definition: elog.c:1317
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define errcontext
Definition: elog.h:196
#define WARNING
Definition: elog.h:36
#define DEBUG1
Definition: elog.h:30
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define NOTICE
Definition: elog.h:35
#define INFO
Definition: elog.h:34
#define ereport(elevel,...)
Definition: elog.h:149
void EventTriggerCollectSimpleCommand(ObjectAddress address, ObjectAddress secondaryObject, Node *parsetree)
#define palloc_object(type)
Definition: fe_memutils.h:74
#define palloc_array(type, count)
Definition: fe_memutils.h:76
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1149
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:127
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:606
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:513
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:387
StrategyNumber GistTranslateStratnum(Oid opclass, CompareType cmptype)
Definition: gistutil.c:1098
bool allowSystemTableMods
Definition: globals.c:129
Oid MyDatabaseTableSpace
Definition: globals.c:95
Oid MyDatabaseId
Definition: globals.c:93
int NewGUCNestLevel(void)
Definition: guc.c:2235
#define newval
void RestrictSearchPath(void)
Definition: guc.c:2246
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:2262
int set_config_option(const char *name, const char *value, GucContext context, GucSource source, GucAction action, bool changeVal, int elevel, bool is_reload)
Definition: guc.c:3342
@ GUC_ACTION_SAVE
Definition: guc.h:201
@ PGC_S_SESSION
Definition: guc.h:122
@ PGC_USERSET
Definition: guc.h:75
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1264
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:778
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:456
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1435
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
#define stmt
Definition: indent_codes.h:59
int verbose
void validate_index(Oid heapId, Oid indexId, Snapshot snapshot)
Definition: index.c:3321
Oid IndexGetRelation(Oid indexId, bool missing_ok)
Definition: index.c:3554
void index_concurrently_set_dead(Oid heapId, Oid indexId)
Definition: index.c:1822
Oid index_create(Relation heapRelation, const char *indexRelationName, Oid indexRelationId, Oid parentIndexRelid, Oid parentConstraintId, RelFileNumber relFileNumber, IndexInfo *indexInfo, const List *indexColNames, Oid accessMethodId, Oid tableSpaceId, const Oid *collationIds, const Oid *opclassIds, const Datum *opclassOptions, const int16 *coloptions, const NullableDatum *stattargets, Datum reloptions, bits16 flags, bits16 constr_flags, bool allow_system_table_mods, bool is_internal, Oid *constraintId)
Definition: index.c:725
void index_concurrently_swap(Oid newIndexId, Oid oldIndexId, const char *oldName)
Definition: index.c:1551
void index_set_state_flags(Oid indexId, IndexStateFlagsAction action)
Definition: index.c:3474
bool CompareIndexInfo(const IndexInfo *info1, const IndexInfo *info2, const Oid *collations1, const Oid *collations2, const Oid *opfamilies1, const Oid *opfamilies2, const AttrMap *attmap)
Definition: index.c:2536
bool reindex_relation(const ReindexStmt *stmt, Oid relid, int flags, const ReindexParams *params)
Definition: index.c:3919
IndexInfo * BuildIndexInfo(Relation index)
Definition: index.c:2427
Oid index_concurrently_create_copy(Relation heapRelation, Oid oldIndexId, Oid tablespaceOid, const char *newName)
Definition: index.c:1299
void index_check_primary_key(Relation heapRel, const IndexInfo *indexInfo, bool is_alter_table, const IndexStmt *stmt)
Definition: index.c:201
void index_concurrently_build(Oid heapRelationId, Oid indexRelationId)
Definition: index.c:1484
void reindex_index(const ReindexStmt *stmt, Oid indexId, bool skip_constraint_checks, char persistence, const ReindexParams *params)
Definition: index.c:3579
#define INDEX_CREATE_IS_PRIMARY
Definition: index.h:61
#define INDEX_CREATE_IF_NOT_EXISTS
Definition: index.h:65
#define REINDEX_REL_PROCESS_TOAST
Definition: index.h:159
#define INDEX_CREATE_PARTITIONED
Definition: index.h:66
#define REINDEXOPT_CONCURRENTLY
Definition: index.h:44
#define REINDEXOPT_MISSING_OK
Definition: index.h:43
#define INDEX_CREATE_INVALID
Definition: index.h:67
#define INDEX_CONSTR_CREATE_WITHOUT_OVERLAPS
Definition: index.h:96
#define INDEX_CREATE_ADD_CONSTRAINT
Definition: index.h:62
#define INDEX_CREATE_SKIP_BUILD
Definition: index.h:63
#define INDEX_CONSTR_CREATE_DEFERRABLE
Definition: index.h:92
#define REINDEXOPT_REPORT_PROGRESS
Definition: index.h:42
@ INDEX_CREATE_SET_VALID
Definition: index.h:27
#define INDEX_CONSTR_CREATE_INIT_DEFERRED
Definition: index.h:93
#define INDEX_CREATE_CONCURRENT
Definition: index.h:64
#define REINDEXOPT_VERBOSE
Definition: index.h:41
#define REINDEX_REL_CHECK_CONSTRAINTS
Definition: index.h:161
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:177
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:133
static bool ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const ReindexParams *params)
Definition: indexcmds.c:3543
void ExecReindex(ParseState *pstate, const ReindexStmt *stmt, bool isTopLevel)
Definition: indexcmds.c:2799
ObjectAddress DefineIndex(Oid tableId, IndexStmt *stmt, Oid indexRelationId, Oid parentIndexId, Oid parentConstraintId, int total_parts, bool is_alter_table, bool check_rights, bool check_not_in_use, bool skip_build, bool quiet)
Definition: indexcmds.c:543
static void set_indexsafe_procflags(void)
Definition: indexcmds.c:4588
char * ChooseRelationName(const char *name1, const char *name2, const char *label, Oid namespaceid, bool isconstraint)
Definition: indexcmds.c:2613
static void reindex_error_callback(void *arg)
Definition: indexcmds.c:3302
static void ComputeIndexAttrs(IndexInfo *indexInfo, Oid *typeOids, Oid *collationOids, Oid *opclassOids, Datum *opclassOptions, int16 *colOptions, const List *attList, const List *exclusionOpNames, Oid relId, const char *accessMethodName, Oid accessMethodId, bool amcanorder, bool isconstraint, bool iswithoutoverlaps, Oid ddl_userid, int ddl_sec_context, int *ddl_save_nestlevel)
Definition: indexcmds.c:1846
static void ReindexIndex(const ReindexStmt *stmt, const ReindexParams *params, bool isTopLevel)
Definition: indexcmds.c:2894
void IndexSetParentIndex(Relation partitionIdx, Oid parentOid)
Definition: indexcmds.c:4418
char * makeObjectName(const char *name1, const char *name2, const char *label)
Definition: indexcmds.c:2527
Oid GetDefaultOpClass(Oid type_id, Oid am_id)
Definition: indexcmds.c:2338
static char * ChooseIndexNameAddition(const List *colnames)
Definition: indexcmds.c:2704
static void ReindexMultipleTables(const ReindexStmt *stmt, const ReindexParams *params)
Definition: indexcmds.c:3083
static bool CompareOpclassOptions(const Datum *opts1, const Datum *opts2, int natts)
Definition: indexcmds.c:363
static void update_relispartition(Oid relationId, bool newval)
Definition: indexcmds.c:4550
bool CheckIndexCompatible(Oid oldId, const char *accessMethodName, const List *attributeList, const List *exclusionOpNames, bool isWithoutOverlaps)
Definition: indexcmds.c:179
void WaitForOlderSnapshots(TransactionId limitXmin, bool progress)
Definition: indexcmds.c:436
Oid ResolveOpClass(const List *opclass, Oid attrType, const char *accessMethodName, Oid accessMethodId)
Definition: indexcmds.c:2253
static void ReindexPartitions(const ReindexStmt *stmt, Oid relid, const ReindexParams *params, bool isTopLevel)
Definition: indexcmds.c:3323
struct ReindexErrorInfo ReindexErrorInfo
static Oid ReindexTable(const ReindexStmt *stmt, const ReindexParams *params, bool isTopLevel)
Definition: indexcmds.c:3024
static void CheckPredicate(Expr *predicate)
Definition: indexcmds.c:1819
static void ReindexMultipleInternal(const ReindexStmt *stmt, const List *relids, const ReindexParams *params)
Definition: indexcmds.c:3417
static void RangeVarCallbackForReindexIndex(const RangeVar *relation, Oid relId, Oid oldRelId, void *arg)
Definition: indexcmds.c:2948
static List * ChooseIndexColumnNames(const List *indexElems)
Definition: indexcmds.c:2738
void GetOperatorFromCompareType(Oid opclass, Oid rhstype, CompareType cmptype, Oid *opid, StrategyNumber *strat)
Definition: indexcmds.c:2440
static char * ChooseIndexName(const char *tabname, Oid namespaceId, const List *colnames, const List *exclusionOpNames, bool primary, bool isconstraint)
Definition: indexcmds.c:2649
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:313
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:365
#define INJECTION_POINT(name)
void CacheInvalidateRelcacheByRelid(Oid relid)
Definition: inval.c:1609
int j
Definition: isn.c:73
int i
Definition: isn.c:72
List * lcons_oid(Oid datum, List *list)
Definition: list.c:531
List * lappend(List *list, void *datum)
Definition: list.c:339
List * list_concat_copy(const List *list1, const List *list2)
Definition: list.c:598
List * lappend_oid(List *list, Oid datum)
Definition: list.c:375
void list_free(List *list)
Definition: list.c:1546
void UnlockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:226
void WaitForLockersMultiple(List *locktags, LOCKMODE lockmode, bool progress)
Definition: lmgr.c:896
void LockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode)
Definition: lmgr.c:386
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:107
void WaitForLockers(LOCKTAG heaplocktag, LOCKMODE lockmode, bool progress)
Definition: lmgr.c:974
void UnlockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode)
Definition: lmgr.c:399
void UnlockTuple(Relation relation, ItemPointer tid, LOCKMODE lockmode)
Definition: lmgr.c:594
bool VirtualXactLock(VirtualTransactionId vxid, bool wait)
Definition: lock.c:4650
#define VirtualTransactionIdIsValid(vxid)
Definition: lock.h:67
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition: lock.h:181
#define VirtualTransactionIdEquals(vxid1, vxid2)
Definition: lock.h:71
#define SetInvalidVirtualTransactionId(vxid)
Definition: lock.h:74
int LOCKMODE
Definition: lockdefs.h:26
#define NoLock
Definition: lockdefs.h:34
#define AccessExclusiveLock
Definition: lockdefs.h:43
#define AccessShareLock
Definition: lockdefs.h:36
#define InplaceUpdateTupleLock
Definition: lockdefs.h:48
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
#define ShareLock
Definition: lockdefs.h:40
#define RowExclusiveLock
Definition: lockdefs.h:38
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1928
char get_rel_persistence(Oid relid)
Definition: lsyscache.c:2078
bool get_index_isvalid(Oid index_oid)
Definition: lsyscache.c:3578
Oid get_opclass_input_type(Oid opclass)
Definition: lsyscache.c:1212
Oid get_opclass_family(Oid opclass)
Definition: lsyscache.c:1190
bool get_opclass_opfamily_and_input_type(Oid opclass, Oid *opfamily, Oid *opcintype)
Definition: lsyscache.c:1235
char * get_opname(Oid opno)
Definition: lsyscache.c:1310
Datum get_attoptions(Oid relid, int16 attnum)
Definition: lsyscache.c:970
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:2003
Oid get_rel_namespace(Oid relid)
Definition: lsyscache.c:1952
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1285
int get_op_opfamily_strategy(Oid opno, Oid opfamily)
Definition: lsyscache.c:83
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:166
bool type_is_collatable(Oid typid)
Definition: lsyscache.c:3081
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2521
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3366
Oid get_relname_relid(const char *relname, Oid relnamespace)
Definition: lsyscache.c:1885
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1509
void op_input_types(Oid opno, Oid *lefttype, Oid *righttype)
Definition: lsyscache.c:1358
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1168
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1781
@ LW_EXCLUSIVE
Definition: lwlock.h:114
IndexInfo * makeIndexInfo(int numattrs, int numkeyattrs, Oid amoid, List *expressions, List *predicates, bool unique, bool nulls_not_distinct, bool isready, bool concurrent, bool summarizing, bool withoutoverlaps)
Definition: makefuncs.c:787
List * make_ands_implicit(Expr *clause)
Definition: makefuncs.c:763
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:1083
char * pstrdup(const char *in)
Definition: mcxt.c:1696
void pfree(void *pointer)
Definition: mcxt.c:1521
void * palloc(Size size)
Definition: mcxt.c:1317
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:454
MemoryContext PortalContext
Definition: mcxt.c:158
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:170
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:466
#define SECURITY_RESTRICTED_OPERATION
Definition: miscadmin.h:318
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
Definition: miscinit.c:660
Oid GetUserId(void)
Definition: miscinit.c:517
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:667
Oid OpclassnameGetOpcid(Oid amid, const char *opcname)
Definition: namespace.c:2121
char * NameListToString(const List *names)
Definition: namespace.c:3594
Oid LookupExplicitNamespace(const char *nspname, bool missing_ok)
Definition: namespace.c:3385
bool isTempNamespace(Oid namespaceId)
Definition: namespace.c:3649
Oid get_collation_oid(List *collname, bool missing_ok)
Definition: namespace.c:3971
void DeconstructQualifiedName(const List *names, char **nspname_p, char **objname_p)
Definition: namespace.c:3301
Oid get_namespace_oid(const char *nspname, bool missing_ok)
Definition: namespace.c:3535
Oid RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, uint32 flags, RangeVarGetRelidCallback callback, void *callback_arg)
Definition: namespace.c:441
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:821
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
const ObjectAddress InvalidObjectAddress
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
TYPCATEGORY TypeCategory(Oid type)
bool IsBinaryCoercible(Oid srctype, Oid targettype)
bool IsPreferredType(TYPCATEGORY category, Oid type)
char TYPCATEGORY
Definition: parse_coerce.h:21
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:106
Oid compatible_oper_opid(List *op, Oid arg1, Oid arg2, bool noError)
Definition: parse_oper.c:487
IndexStmt * generateClonedIndexStmt(RangeVar *heapRel, Relation source_idx, const AttrMap *attmap, Oid *constraintOid)
@ SORTBY_NULLS_DEFAULT
Definition: parsenodes.h:54
@ SORTBY_NULLS_FIRST
Definition: parsenodes.h:55
#define ACL_MAINTAIN
Definition: parsenodes.h:90
@ PARTITION_STRATEGY_HASH
Definition: parsenodes.h:885
@ DROP_RESTRICT
Definition: parsenodes.h:2385
@ OBJECT_SCHEMA
Definition: parsenodes.h:2348
@ OBJECT_TABLESPACE
Definition: parsenodes.h:2354
@ OBJECT_INDEX
Definition: parsenodes.h:2332
@ OBJECT_DATABASE
Definition: parsenodes.h:2321
ReindexObjectType
Definition: parsenodes.h:4041
@ REINDEX_OBJECT_DATABASE
Definition: parsenodes.h:4046
@ REINDEX_OBJECT_INDEX
Definition: parsenodes.h:4042
@ REINDEX_OBJECT_SCHEMA
Definition: parsenodes.h:4044
@ REINDEX_OBJECT_SYSTEM
Definition: parsenodes.h:4045
@ REINDEX_OBJECT_TABLE
Definition: parsenodes.h:4043
#define ACL_CREATE
Definition: parsenodes.h:85
@ SORTBY_DESC
Definition: parsenodes.h:48
@ SORTBY_DEFAULT
Definition: parsenodes.h:46
PartitionKey RelationGetPartitionKey(Relation rel)
Definition: partcache.c:51
PartitionDesc RelationGetPartitionDesc(Relation rel, bool omit_detached)
Definition: partdesc.c:71
FormData_pg_am * Form_pg_am
Definition: pg_am.h:48
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
static void fix_dependencies(ArchiveHandle *AH)
void * arg
static char * label
int errdetail_relkind_not_supported(char relkind)
Definition: pg_class.c:24
NameData relname
Definition: pg_class.h:38
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
#define INDEX_MAX_KEYS
#define NAMEDATALEN
Oid get_relation_idx_constraint_oid(Oid relationId, Oid indexId)
void ConstraintSetParentConstraint(Oid childConstrId, Oid parentConstrId, Oid childTableId)
bool ConstraintNameExists(const char *conname, Oid namespaceid)
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:45
long deleteDependencyRecordsForClass(Oid classId, Oid objectId, Oid refclassId, char deptype)
Definition: pg_depend.c:351
FormData_pg_index * Form_pg_index
Definition: pg_index.h:70
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
Definition: pg_inherits.c:255
void StoreSingleInheritance(Oid relationId, Oid parentOid, int32 seqNumber)
Definition: pg_inherits.c:508
bool has_superclass(Oid relationId)
Definition: pg_inherits.c:377
FormData_pg_inherits * Form_pg_inherits
Definition: pg_inherits.h:45
#define lfirst(lc)
Definition: pg_list.h:172
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:518
#define list_make1_oid(x1)
Definition: pg_list.h:242
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
#define lfirst_oid(lc)
Definition: pg_list.h:174
FormData_pg_opclass * Form_pg_opclass
Definition: pg_opclass.h:83
FormData_pg_opfamily * Form_pg_opfamily
Definition: pg_opfamily.h:51
const char * pg_rusage_show(const PGRUsage *ru0)
Definition: pg_rusage.c:40
void pg_rusage_init(PGRUsage *ru0)
Definition: pg_rusage.c:27
static char * buf
Definition: pg_test_fsync.c:72
static int progress
Definition: pgbench.c:261
static int partitions
Definition: pgbench.c:223
#define sprintf
Definition: port.h:240
#define snprintf
Definition: port.h:238
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
static bool DatumGetBool(Datum X)
Definition: postgres.h:95
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
uintptr_t Datum
Definition: postgres.h:69
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:317
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:217
#define InvalidOid
Definition: postgres_ext.h:37
unsigned int Oid
Definition: postgres_ext.h:32
CompareType
Definition: primnodes.h:1470
@ COMPARE_OVERLAP
Definition: primnodes.h:1477
@ COMPARE_EQ
Definition: primnodes.h:1473
@ COMPARE_CONTAINED_BY
Definition: primnodes.h:1478
#define PROC_IN_SAFE_IC
Definition: proc.h:59
#define PROC_IN_VACUUM
Definition: proc.h:58
#define PROC_IS_AUTOVACUUM
Definition: proc.h:57
VirtualTransactionId * GetCurrentVirtualXIDs(TransactionId limitXmin, bool excludeXmin0, bool allDbs, int excludeVacuum, int *nvxids)
Definition: procarray.c:3324
PGPROC * ProcNumberGetProc(ProcNumber procNumber)
Definition: procarray.c:3138
#define PROGRESS_CREATEIDX_PHASE_WAIT_4
Definition: progress.h:101
#define PROGRESS_CREATEIDX_PHASE_BUILD
Definition: progress.h:95
#define PROGRESS_CREATEIDX_PARTITIONS_DONE
Definition: progress.h:90
#define PROGRESS_CREATEIDX_PHASE_WAIT_1
Definition: progress.h:94
#define PROGRESS_CREATEIDX_COMMAND_CREATE_CONCURRENTLY
Definition: progress.h:112
#define PROGRESS_CREATEIDX_ACCESS_METHOD_OID
Definition: progress.h:84
#define PROGRESS_WAITFOR_DONE
Definition: progress.h:118
#define PROGRESS_CREATEIDX_PHASE_WAIT_3
Definition: progress.h:100
#define PROGRESS_WAITFOR_TOTAL
Definition: progress.h:117
#define PROGRESS_CREATEIDX_COMMAND_REINDEX_CONCURRENTLY
Definition: progress.h:114
#define PROGRESS_CREATEIDX_COMMAND_CREATE
Definition: progress.h:111
#define PROGRESS_WAITFOR_CURRENT_PID
Definition: progress.h:119
#define PROGRESS_CREATEIDX_PHASE_WAIT_2
Definition: progress.h:96
#define PROGRESS_CREATEIDX_PHASE
Definition: progress.h:85
#define PROGRESS_CREATEIDX_PHASE_VALIDATE_IDXSCAN
Definition: progress.h:97
#define PROGRESS_CREATEIDX_PHASE_WAIT_5
Definition: progress.h:102
#define PROGRESS_CREATEIDX_INDEX_OID
Definition: progress.h:83
#define PROGRESS_CREATEIDX_PARTITIONS_TOTAL
Definition: progress.h:89
#define PROGRESS_CREATEIDX_COMMAND
Definition: progress.h:82
char * format_operator(Oid operator_oid)
Definition: regproc.c:793
#define RelationGetRelid(relation)
Definition: rel.h:505
#define RelationGetDescr(relation)
Definition: rel.h:531
#define RelationGetRelationName(relation)
Definition: rel.h:539
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:658
#define RelationGetNamespace(relation)
Definition: rel.h:546
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4756
List * RelationGetIndexPredicate(Relation relation)
Definition: relcache.c:5130
List * RelationGetIndexExpressions(Relation relation)
Definition: relcache.c:5017
void RelationGetExclusionInfo(Relation indexRelation, Oid **operators, Oid **procs, uint16 **strategies)
Definition: relcache.c:5573
Datum transformRelOptions(Datum oldOptions, List *defList, const char *namspace, const char *const validnsps[], bool acceptOidsOff, bool isReset)
Definition: reloptions.c:1156
bytea * index_reloptions(amoptions_function amoptions, Datum reloptions, bool validate)
Definition: reloptions.c:2054
#define RelFileNumberIsValid(relnumber)
Definition: relpath.h:27
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
@ ForwardScanDirection
Definition: sdir.h:28
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:212
void UnregisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:794
void PushActiveSnapshot(Snapshot snapshot)
Definition: snapmgr.c:610
bool ActiveSnapshotSet(void)
Definition: snapmgr.c:740
Snapshot RegisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:752
void PopActiveSnapshot(void)
Definition: snapmgr.c:703
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:205
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:47
PGPROC * MyProc
Definition: proc.c:66
PROC_HDR * ProcGlobal
Definition: proc.c:78
uint16 StrategyNumber
Definition: stratnum.h:22
#define InvalidStrategy
Definition: stratnum.h:24
#define HTEqualStrategyNumber
Definition: stratnum.h:41
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:30
Definition: attmap.h:35
char * defname
Definition: parsenodes.h:826
ParseLoc location
Definition: parsenodes.h:830
struct ErrorContextCallback * previous
Definition: elog.h:296
void(* callback)(void *arg)
Definition: elog.h:297
Definition: fmgr.h:57
ItemPointerData t_self
Definition: htup.h:65
amoptions_function amoptions
Definition: amapi.h:287
amgettuple_function amgettuple
Definition: amapi.h:294
bool amcanunique
Definition: amapi.h:241
bool amsummarizing
Definition: amapi.h:265
bool amcanmulticol
Definition: amapi.h:243
bool amcanorder
Definition: amapi.h:235
bool amcaninclude
Definition: amapi.h:261
Node * expr
Definition: parsenodes.h:795
SortByDir ordering
Definition: parsenodes.h:800
List * opclassopts
Definition: parsenodes.h:799
char * indexcolname
Definition: parsenodes.h:796
SortByNulls nulls_ordering
Definition: parsenodes.h:801
List * opclass
Definition: parsenodes.h:798
char * name
Definition: parsenodes.h:794
List * collation
Definition: parsenodes.h:797
uint16 * ii_ExclusionStrats
Definition: execnodes.h:204
int ii_NumIndexAttrs
Definition: execnodes.h:195
Oid * ii_ExclusionOps
Definition: execnodes.h:202
int ii_NumIndexKeyAttrs
Definition: execnodes.h:196
List * ii_Expressions
Definition: execnodes.h:198
Oid * ii_ExclusionProcs
Definition: execnodes.h:203
AttrNumber ii_IndexAttrNumbers[INDEX_MAX_KEYS]
Definition: execnodes.h:197
List * ii_Predicate
Definition: execnodes.h:200
Definition: lock.h:165
Definition: pg_list.h:54
LockRelId lockRelId
Definition: rel.h:46
Definition: rel.h:39
Oid relId
Definition: rel.h:40
Oid dbId
Definition: rel.h:41
Definition: nodes.h:129