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