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