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