LCOV - code coverage report
Current view: top level - src/backend/commands - indexcmds.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 1237 1336 92.6 %
Date: 2025-02-21 17:14:59 Functions: 26 26 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.14