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