LCOV - differential code coverage report
Current view: top level - src/backend/commands - indexcmds.c (source / functions) Coverage Total Hit UBC CBC
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 92.8 % 1364 1266 98 1266
Current Date: 2025-09-06 07:49:51 +0900 Functions: 100.0 % 26 26 26
Baseline: lcov-20250906-005545-baseline Branches: 68.6 % 1199 822 377 822
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 93.2 % 74 69 5 69
(360..) days: 92.8 % 1290 1197 93 1197
Function coverage date bins:
(30,360] days: 100.0 % 1 1 1
(360..) days: 100.0 % 25 25 25
Branch coverage date bins:
(30,360] days: 61.2 % 98 60 38 60
(360..) days: 69.2 % 1101 762 339 762

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

Generated by: LCOV version 2.4-beta