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