LCOV - differential code coverage report
Current view: top level - src/backend/commands - indexcmds.c (source / functions) Coverage Total Hit UNC LBC UIC UBC GBC GIC GNC CBC DUB DCB
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 91.8 % 1357 1246 11 1 99 9 7 132 1098 5 81
Current Date: 2024-04-14 14:21:10 Functions: 100.0 % 26 26 17 9 12
Baseline: 16@8cea358b128 Branches: 67.6 % 1211 819 61 2 1 328 11 1 85 722
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed [..60] days: 95.0 % 20 19 1 19
(60,120] days: 78.4 % 37 29 8 29
(120,180] days: 97.0 % 33 32 1 30 2
(180,240] days: 95.8 % 48 46 2 46
(240..) days: 91.9 % 1219 1120 1 98 9 7 8 1096
Function coverage date bins:
[..60] days: 100.0 % 1 1 1
(120,180] days: 100.0 % 6 6 6
(180,240] days: 100.0 % 6 6 6
(240..) days: 100.0 % 13 13 4 9
Branch coverage date bins:
[..60] days: 75.0 % 20 15 5 15
(60,120] days: 56.8 % 74 42 32 42
(120,180] days: 70.0 % 10 7 2 1 1 4 2
(180,240] days: 52.6 % 38 20 18 20
(240..) days: 68.8 % 1069 735 4 2 1 327 10 1 4 720

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

Generated by: LCOV version 2.1-beta2-3-g6141622