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