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