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

Generated by: LCOV version 1.14