LCOV - differential code coverage report
Current view: top level - src/backend/statistics - stat_utils.c (source / functions) Coverage Total Hit UNC UBC GBC GNC CBC
Current: 0e5ff9b9b45a657aea12440478dc002e9b01f138 vs 0123ce131fca454009439dfa3b2266d1d40737d7 Lines: 91.5 % 236 216 11 9 5 127 84
Current Date: 2026-03-14 14:10:32 -0400 Functions: 100.0 % 13 13 6 7
Baseline: lcov-20260315-024220-baseline Branches: 69.1 % 178 123 25 30 3 47 73
Baseline Date: 2026-03-14 15:27:56 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 90.4 % 167 151 11 5 3 127 21
(360..) days: 94.2 % 69 65 4 2 63
Function coverage date bins:
(30,360] days: 100.0 % 7 7 6 1
(360..) days: 100.0 % 6 6 6
Branch coverage date bins:
(30,360] days: 63.9 % 108 69 25 14 47 22
(360..) days: 77.1 % 70 54 16 3 51

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  * stat_utils.c
                                  3                 :                :  *
                                  4                 :                :  *    PostgreSQL statistics manipulation utilities.
                                  5                 :                :  *
                                  6                 :                :  * Code supporting the direct manipulation of statistics.
                                  7                 :                :  *
                                  8                 :                :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
                                  9                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                 10                 :                :  *
                                 11                 :                :  * IDENTIFICATION
                                 12                 :                :  *       src/backend/statistics/stat_utils.c
                                 13                 :                :  *
                                 14                 :                :  *-------------------------------------------------------------------------
                                 15                 :                :  */
                                 16                 :                : 
                                 17                 :                : #include "postgres.h"
                                 18                 :                : 
                                 19                 :                : #include "access/htup_details.h"
                                 20                 :                : #include "access/relation.h"
                                 21                 :                : #include "catalog/index.h"
                                 22                 :                : #include "catalog/namespace.h"
                                 23                 :                : #include "catalog/pg_class.h"
                                 24                 :                : #include "catalog/pg_collation.h"
                                 25                 :                : #include "catalog/pg_database.h"
                                 26                 :                : #include "catalog/pg_statistic.h"
                                 27                 :                : #include "funcapi.h"
                                 28                 :                : #include "miscadmin.h"
                                 29                 :                : #include "nodes/nodeFuncs.h"
                                 30                 :                : #include "statistics/stat_utils.h"
                                 31                 :                : #include "storage/lmgr.h"
                                 32                 :                : #include "utils/acl.h"
                                 33                 :                : #include "utils/array.h"
                                 34                 :                : #include "utils/builtins.h"
                                 35                 :                : #include "utils/lsyscache.h"
                                 36                 :                : #include "utils/rel.h"
                                 37                 :                : #include "utils/syscache.h"
                                 38                 :                : #include "utils/typcache.h"
                                 39                 :                : 
                                 40                 :                : /* Default values assigned to new pg_statistic tuples. */
                                 41                 :                : #define DEFAULT_STATATT_NULL_FRAC      Float4GetDatum(0.0)  /* stanullfrac */
                                 42                 :                : #define DEFAULT_STATATT_AVG_WIDTH      Int32GetDatum(0) /* stawidth, same as
                                 43                 :                :                                                          * unknown */
                                 44                 :                : #define DEFAULT_STATATT_N_DISTINCT     Float4GetDatum(0.0)  /* stadistinct, same as
                                 45                 :                :                                                              * unknown */
                                 46                 :                : 
                                 47                 :                : static Node *statatt_get_index_expr(Relation rel, int attnum);
                                 48                 :                : 
                                 49                 :                : /*
                                 50                 :                :  * Ensure that a given argument is not null.
                                 51                 :                :  */
                                 52                 :                : void
  520 jdavis@postgresql.or       53                 :CBC        5886 : stats_check_required_arg(FunctionCallInfo fcinfo,
                                 54                 :                :                          struct StatsArgInfo *arginfo,
                                 55                 :                :                          int argnum)
                                 56                 :                : {
                                 57         [ +  + ]:           5886 :     if (PG_ARGISNULL(argnum))
                                 58         [ +  - ]:             54 :         ereport(ERROR,
                                 59                 :                :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                 60                 :                :                  errmsg("argument \"%s\" must not be null",
                                 61                 :                :                         arginfo[argnum].argname)));
                                 62                 :           5832 : }
                                 63                 :                : 
                                 64                 :                : /*
                                 65                 :                :  * Check that argument is either NULL or a one dimensional array with no
                                 66                 :                :  * NULLs.
                                 67                 :                :  *
                                 68                 :                :  * If a problem is found, emit a WARNING, and return false. Otherwise return
                                 69                 :                :  * true.
                                 70                 :                :  */
                                 71                 :                : bool
  509                            72                 :           2331 : stats_check_arg_array(FunctionCallInfo fcinfo,
                                 73                 :                :                       struct StatsArgInfo *arginfo,
                                 74                 :                :                       int argnum)
                                 75                 :                : {
                                 76                 :                :     ArrayType  *arr;
                                 77                 :                : 
                                 78         [ +  + ]:           2331 :     if (PG_ARGISNULL(argnum))
                                 79                 :           1904 :         return true;
                                 80                 :                : 
                                 81                 :            427 :     arr = DatumGetArrayTypeP(PG_GETARG_DATUM(argnum));
                                 82                 :                : 
                                 83         [ -  + ]:            427 :     if (ARR_NDIM(arr) != 1)
                                 84                 :                :     {
  383 jdavis@postgresql.or       85         [ #  # ]:UBC           0 :         ereport(WARNING,
                                 86                 :                :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                 87                 :                :                  errmsg("argument \"%s\" must not be a multidimensional array",
                                 88                 :                :                         arginfo[argnum].argname)));
  509                            89                 :              0 :         return false;
                                 90                 :                :     }
                                 91                 :                : 
  509 jdavis@postgresql.or       92         [ +  + ]:CBC         427 :     if (array_contains_nulls(arr))
                                 93                 :                :     {
  383                            94         [ +  - ]:              3 :         ereport(WARNING,
                                 95                 :                :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                 96                 :                :                  errmsg("argument \"%s\" array must not contain null values",
                                 97                 :                :                         arginfo[argnum].argname)));
  509                            98                 :              3 :         return false;
                                 99                 :                :     }
                                100                 :                : 
                                101                 :            424 :     return true;
                                102                 :                : }
                                103                 :                : 
                                104                 :                : /*
                                105                 :                :  * Enforce parameter pairs that must be specified together (or not at all) for
                                106                 :                :  * a particular stakind, such as most_common_vals and most_common_freqs for
                                107                 :                :  * STATISTIC_KIND_MCV.
                                108                 :                :  *
                                109                 :                :  * If a problem is found, emit a WARNING, and return false. Otherwise return
                                110                 :                :  * true.
                                111                 :                :  */
                                112                 :                : bool
                                113                 :           2331 : stats_check_arg_pair(FunctionCallInfo fcinfo,
                                114                 :                :                      struct StatsArgInfo *arginfo,
                                115                 :                :                      int argnum1, int argnum2)
                                116                 :                : {
                                117   [ +  +  +  + ]:           2331 :     if (PG_ARGISNULL(argnum1) && PG_ARGISNULL(argnum2))
                                118                 :           1892 :         return true;
                                119                 :                : 
                                120   [ +  +  +  + ]:            439 :     if (PG_ARGISNULL(argnum1) || PG_ARGISNULL(argnum2))
                                121                 :                :     {
                                122         [ +  + ]:             21 :         int         nullarg = PG_ARGISNULL(argnum1) ? argnum1 : argnum2;
                                123         [ +  + ]:             21 :         int         otherarg = PG_ARGISNULL(argnum1) ? argnum2 : argnum1;
                                124                 :                : 
  383                           125         [ +  - ]:             21 :         ereport(WARNING,
                                126                 :                :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                127                 :                :                  errmsg("argument \"%s\" must be specified when argument \"%s\" is specified",
                                128                 :                :                         arginfo[nullarg].argname,
                                129                 :                :                         arginfo[otherarg].argname)));
                                130                 :                : 
  509                           131                 :             21 :         return false;
                                132                 :                :     }
                                133                 :                : 
                                134                 :            418 :     return true;
                                135                 :                : }
                                136                 :                : 
                                137                 :                : /*
                                138                 :                :  * A role has privileges to set statistics on the relation if any of the
                                139                 :                :  * following are true:
                                140                 :                :  *   - the role owns the current database and the relation is not shared
                                141                 :                :  *   - the role has the MAINTAIN privilege on the relation
                                142                 :                :  */
                                143                 :                : void
  151 nathan@postgresql.or      144                 :           2128 : RangeVarCallbackForStats(const RangeVar *relation,
                                145                 :                :                          Oid relId, Oid oldRelId, void *arg)
                                146                 :                : {
                                147                 :           2128 :     Oid        *locked_oid = (Oid *) arg;
                                148                 :           2128 :     Oid         table_oid = relId;
                                149                 :                :     HeapTuple   tuple;
                                150                 :                :     Form_pg_class form;
                                151                 :                :     char        relkind;
                                152                 :                : 
                                153                 :                :     /*
                                154                 :                :      * If we previously locked some other index's heap, and the name we're
                                155                 :                :      * looking up no longer refers to that relation, release the now-useless
                                156                 :                :      * lock.
                                157                 :                :      */
                                158   [ +  +  -  + ]:           2128 :     if (relId != oldRelId && OidIsValid(*locked_oid))
                                159                 :                :     {
  151 nathan@postgresql.or      160                 :UBC           0 :         UnlockRelationOid(*locked_oid, ShareUpdateExclusiveLock);
                                161                 :              0 :         *locked_oid = InvalidOid;
                                162                 :                :     }
                                163                 :                : 
                                164                 :                :     /* If the relation does not exist, there's nothing more to do. */
  151 nathan@postgresql.or      165         [ +  + ]:CBC        2128 :     if (!OidIsValid(relId))
                                166                 :             12 :         return;
                                167                 :                : 
                                168                 :                :     /* If the relation does exist, check whether it's an index. */
                                169                 :           2116 :     relkind = get_rel_relkind(relId);
                                170   [ +  +  +  + ]:           2116 :     if (relkind == RELKIND_INDEX ||
                                171                 :                :         relkind == RELKIND_PARTITIONED_INDEX)
                                172                 :            301 :         table_oid = IndexGetRelation(relId, false);
                                173                 :                : 
                                174                 :                :     /*
                                175                 :                :      * If retrying yields the same OID, there are a couple of extremely
                                176                 :                :      * unlikely scenarios we need to handle.
                                177                 :                :      */
                                178         [ +  + ]:           2116 :     if (relId == oldRelId)
                                179                 :                :     {
                                180                 :                :         /*
                                181                 :                :          * If a previous lookup found an index, but the current lookup did
                                182                 :                :          * not, the index was dropped and the OID was reused for something
                                183                 :                :          * else between lookups.  In theory, we could simply drop our lock on
                                184                 :                :          * the index's parent table and proceed, but in the interest of
                                185                 :                :          * avoiding complexity, we just error.
                                186                 :                :          */
                                187   [ -  +  -  - ]:              1 :         if (table_oid == relId && OidIsValid(*locked_oid))
  151 nathan@postgresql.or      188         [ #  # ]:UBC           0 :             ereport(ERROR,
                                189                 :                :                     (errcode(ERRCODE_UNDEFINED_OBJECT),
                                190                 :                :                      errmsg("index \"%s\" was concurrently dropped",
                                191                 :                :                             relation->relname)));
                                192                 :                : 
                                193                 :                :         /*
                                194                 :                :          * If the current lookup found an index but a previous lookup either
                                195                 :                :          * did not find an index or found one with a different parent
                                196                 :                :          * relation, the relation was dropped and the OID was reused for an
                                197                 :                :          * index between lookups.  RangeVarGetRelidExtended() will have
                                198                 :                :          * already locked the index at this point, so we can't just lock the
                                199                 :                :          * newly discovered parent table OID without risking deadlock.  As
                                200                 :                :          * above, we just error in this case.
                                201                 :                :          */
  151 nathan@postgresql.or      202   [ +  -  -  + ]:CBC           1 :         if (table_oid != relId && table_oid != *locked_oid)
  151 nathan@postgresql.or      203         [ #  # ]:UBC           0 :             ereport(ERROR,
                                204                 :                :                     (errcode(ERRCODE_UNDEFINED_OBJECT),
                                205                 :                :                      errmsg("index \"%s\" was concurrently created",
                                206                 :                :                             relation->relname)));
                                207                 :                :     }
                                208                 :                : 
  151 nathan@postgresql.or      209                 :CBC        2116 :     tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(table_oid));
                                210         [ -  + ]:           2116 :     if (!HeapTupleIsValid(tuple))
  151 nathan@postgresql.or      211         [ #  # ]:UBC           0 :         elog(ERROR, "cache lookup failed for OID %u", table_oid);
  151 nathan@postgresql.or      212                 :CBC        2116 :     form = (Form_pg_class) GETSTRUCT(tuple);
                                213                 :                : 
                                214                 :                :     /* the relkinds that can be used with ANALYZE */
                                215         [ +  + ]:           2116 :     switch (form->relkind)
                                216                 :                :     {
  520 jdavis@postgresql.or      217                 :           2107 :         case RELKIND_RELATION:
                                218                 :                :         case RELKIND_MATVIEW:
                                219                 :                :         case RELKIND_FOREIGN_TABLE:
                                220                 :                :         case RELKIND_PARTITIONED_TABLE:
                                221                 :           2107 :             break;
                                222                 :              9 :         default:
                                223         [ +  - ]:              9 :             ereport(ERROR,
                                224                 :                :                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                                225                 :                :                      errmsg("cannot modify statistics for relation \"%s\"",
                                226                 :                :                             NameStr(form->relname)),
                                227                 :                :                      errdetail_relkind_not_supported(form->relkind)));
                                228                 :                :     }
                                229                 :                : 
  151 nathan@postgresql.or      230         [ -  + ]:           2107 :     if (form->relisshared)
  520 jdavis@postgresql.or      231         [ #  # ]:UBC           0 :         ereport(ERROR,
                                232                 :                :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                233                 :                :                  errmsg("cannot modify statistics for shared relation")));
                                234                 :                : 
                                235                 :                :     /* Check permissions */
  520 jdavis@postgresql.or      236         [ +  + ]:CBC        2107 :     if (!object_ownercheck(DatabaseRelationId, MyDatabaseId, GetUserId()))
                                237                 :                :     {
  151 nathan@postgresql.or      238                 :GBC          12 :         AclResult   aclresult = pg_class_aclcheck(table_oid,
                                239                 :                :                                                   GetUserId(),
                                240                 :                :                                                   ACL_MAINTAIN);
                                241                 :                : 
  520 jdavis@postgresql.or      242         [ +  + ]:             12 :         if (aclresult != ACLCHECK_OK)
                                243                 :              6 :             aclcheck_error(aclresult,
  151 nathan@postgresql.or      244                 :              6 :                            get_relkind_objtype(form->relkind),
                                245                 :              6 :                            NameStr(form->relname));
                                246                 :                :     }
                                247                 :                : 
  151 nathan@postgresql.or      248                 :CBC        2101 :     ReleaseSysCache(tuple);
                                249                 :                : 
                                250                 :                :     /* Lock heap before index to avoid deadlock. */
                                251   [ +  +  +  + ]:           2101 :     if (relId != oldRelId && table_oid != relId)
                                252                 :                :     {
                                253                 :            300 :         LockRelationOid(table_oid, ShareUpdateExclusiveLock);
                                254                 :            300 :         *locked_oid = table_oid;
                                255                 :                :     }
                                256                 :                : }
                                257                 :                : 
                                258                 :                : 
                                259                 :                : /*
                                260                 :                :  * Find the argument number for the given argument name, returning -1 if not
                                261                 :                :  * found.
                                262                 :                :  */
                                263                 :                : static int
  383 jdavis@postgresql.or      264                 :          15215 : get_arg_by_name(const char *argname, struct StatsArgInfo *arginfo)
                                265                 :                : {
                                266                 :                :     int         argnum;
                                267                 :                : 
  507                           268         [ +  + ]:          74000 :     for (argnum = 0; arginfo[argnum].argname != NULL; argnum++)
                                269         [ +  + ]:          73994 :         if (pg_strcasecmp(argname, arginfo[argnum].argname) == 0)
                                270                 :          15209 :             return argnum;
                                271                 :                : 
  383                           272         [ +  - ]:              6 :     ereport(WARNING,
                                273                 :                :             (errmsg("unrecognized argument name: \"%s\"", argname)));
                                274                 :                : 
  507                           275                 :              6 :     return -1;
                                276                 :                : }
                                277                 :                : 
                                278                 :                : /*
                                279                 :                :  * Ensure that a given argument matched the expected type.
                                280                 :                :  */
                                281                 :                : static bool
  383                           282                 :          15209 : stats_check_arg_type(const char *argname, Oid argtype, Oid expectedtype)
                                283                 :                : {
  507                           284         [ +  + ]:          15209 :     if (argtype != expectedtype)
                                285                 :                :     {
  383                           286         [ +  - ]:             12 :         ereport(WARNING,
                                287                 :                :                 (errmsg("argument \"%s\" has type %s, expected type %s",
                                288                 :                :                         argname, format_type_be(argtype),
                                289                 :                :                         format_type_be(expectedtype))));
  507                           290                 :             12 :         return false;
                                291                 :                :     }
                                292                 :                : 
                                293                 :          15197 :     return true;
                                294                 :                : }
                                295                 :                : 
                                296                 :                : /*
                                297                 :                :  * Check if attribute of an index is an expression, then retrieve the
                                298                 :                :  * expression if is it the case.
                                299                 :                :  *
                                300                 :                :  * If the attnum specified is known to be an expression, then we must
                                301                 :                :  * walk the list attributes up to the specified attnum to get the right
                                302                 :                :  * expression.
                                303                 :                :  */
                                304                 :                : static Node *
   80 michael@paquier.xyz       305                 :GNC         777 : statatt_get_index_expr(Relation rel, int attnum)
                                306                 :                : {
                                307                 :                :     List       *index_exprs;
                                308                 :                :     ListCell   *indexpr_item;
                                309                 :                : 
                                310                 :                :     /* relation is not an index */
                                311         [ +  + ]:            777 :     if (rel->rd_rel->relkind != RELKIND_INDEX &&
                                312         [ +  - ]:            770 :         rel->rd_rel->relkind != RELKIND_PARTITIONED_INDEX)
                                313                 :            770 :         return NULL;
                                314                 :                : 
                                315                 :              7 :     index_exprs = RelationGetIndexExpressions(rel);
                                316                 :                : 
                                317                 :                :     /* index has no expressions to give */
                                318         [ -  + ]:              7 :     if (index_exprs == NIL)
   80 michael@paquier.xyz       319                 :UNC           0 :         return NULL;
                                320                 :                : 
                                321                 :                :     /*
                                322                 :                :      * The index's attnum points directly to a relation attnum, hence it is
                                323                 :                :      * not an expression attribute.
                                324                 :                :      */
   80 michael@paquier.xyz       325         [ -  + ]:GNC           7 :     if (rel->rd_index->indkey.values[attnum - 1] != 0)
   80 michael@paquier.xyz       326                 :UNC           0 :         return NULL;
                                327                 :                : 
   80 michael@paquier.xyz       328                 :GNC           7 :     indexpr_item = list_head(rel->rd_indexprs);
                                329                 :                : 
                                330         [ -  + ]:              7 :     for (int i = 0; i < attnum - 1; i++)
   80 michael@paquier.xyz       331         [ #  # ]:UNC           0 :         if (rel->rd_index->indkey.values[i] == 0)
                                332                 :              0 :             indexpr_item = lnext(rel->rd_indexprs, indexpr_item);
                                333                 :                : 
   80 michael@paquier.xyz       334         [ -  + ]:GNC           7 :     if (indexpr_item == NULL)   /* shouldn't happen */
   80 michael@paquier.xyz       335         [ #  # ]:UNC           0 :         elog(ERROR, "too few entries in indexprs list");
                                336                 :                : 
   80 michael@paquier.xyz       337                 :GNC           7 :     return (Node *) lfirst(indexpr_item);
                                338                 :                : }
                                339                 :                : 
                                340                 :                : /*
                                341                 :                :  * Translate variadic argument pairs from 'pairs_fcinfo' into a
                                342                 :                :  * 'positional_fcinfo' appropriate for calling relation_statistics_update() or
                                343                 :                :  * attribute_statistics_update() with positional arguments.
                                344                 :                :  *
                                345                 :                :  * Caller should have already initialized positional_fcinfo with a size
                                346                 :                :  * appropriate for calling the intended positional function, and arginfo
                                347                 :                :  * should also match the intended positional function.
                                348                 :                :  */
                                349                 :                : bool
  507 jdavis@postgresql.or      350                 :CBC        2136 : stats_fill_fcinfo_from_arg_pairs(FunctionCallInfo pairs_fcinfo,
                                351                 :                :                                  FunctionCallInfo positional_fcinfo,
                                352                 :                :                                  struct StatsArgInfo *arginfo)
                                353                 :                : {
                                354                 :                :     Datum      *args;
                                355                 :                :     bool       *argnulls;
                                356                 :                :     Oid        *types;
                                357                 :                :     int         nargs;
                                358                 :           2136 :     bool        result = true;
                                359                 :                : 
                                360                 :                :     /* clear positional args */
                                361         [ +  + ]:          25787 :     for (int i = 0; arginfo[i].argname != NULL; i++)
                                362                 :                :     {
                                363                 :          23651 :         positional_fcinfo->args[i].value = (Datum) 0;
                                364                 :          23651 :         positional_fcinfo->args[i].isnull = true;
                                365                 :                :     }
                                366                 :                : 
                                367                 :           2136 :     nargs = extract_variadic_args(pairs_fcinfo, 0, true,
                                368                 :                :                                   &args, &types, &argnulls);
                                369                 :                : 
                                370         [ +  + ]:           2136 :     if (nargs % 2 != 0)
                                371         [ +  - ]:              3 :         ereport(ERROR,
                                372                 :                :                 errmsg("variadic arguments must be name/value pairs"),
                                373                 :                :                 errhint("Provide an even number of variadic arguments that can be divided into pairs."));
                                374                 :                : 
                                375                 :                :     /*
                                376                 :                :      * For each argument name/value pair, find corresponding positional
                                377                 :                :      * argument for the argument name, and assign the argument value to
                                378                 :                :      * positional_fcinfo.
                                379                 :                :      */
                                380         [ +  + ]:          19259 :     for (int i = 0; i < nargs; i += 2)
                                381                 :                :     {
                                382                 :                :         int         argnum;
                                383                 :                :         char       *argname;
                                384                 :                : 
                                385         [ +  + ]:          17129 :         if (argnulls[i])
                                386         [ +  - ]:              3 :             ereport(ERROR,
                                387                 :                :                     (errmsg("name at variadic position %d is null", i + 1)));
                                388                 :                : 
                                389         [ -  + ]:          17126 :         if (types[i] != TEXTOID)
  507 jdavis@postgresql.or      390         [ #  # ]:UBC           0 :             ereport(ERROR,
                                391                 :                :                     (errmsg("name at variadic position %d has type %s, expected type %s",
                                392                 :                :                             i + 1, format_type_be(types[i]),
                                393                 :                :                             format_type_be(TEXTOID))));
                                394                 :                : 
  507 jdavis@postgresql.or      395         [ +  + ]:CBC       17126 :         if (argnulls[i + 1])
                                396                 :            153 :             continue;
                                397                 :                : 
                                398                 :          16973 :         argname = TextDatumGetCString(args[i]);
                                399                 :                : 
                                400                 :                :         /*
                                401                 :                :          * The 'version' argument is a special case, not handled by arginfo
                                402                 :                :          * because it's not a valid positional argument.
                                403                 :                :          *
                                404                 :                :          * For now, 'version' is accepted but ignored. In the future it can be
                                405                 :                :          * used to interpret older statistics properly.
                                406                 :                :          */
                                407         [ +  + ]:          16973 :         if (pg_strcasecmp(argname, "version") == 0)
                                408                 :           1758 :             continue;
                                409                 :                : 
  383                           410                 :          15215 :         argnum = get_arg_by_name(argname, arginfo);
                                411                 :                : 
  507                           412   [ +  +  +  + ]:          30424 :         if (argnum < 0 || !stats_check_arg_type(argname, types[i + 1],
  383                           413                 :          15209 :                                                 arginfo[argnum].argtype))
                                414                 :                :         {
  507                           415                 :             18 :             result = false;
                                416                 :             18 :             continue;
                                417                 :                :         }
                                418                 :                : 
                                419                 :          15197 :         positional_fcinfo->args[argnum].value = args[i + 1];
                                420                 :          15197 :         positional_fcinfo->args[argnum].isnull = false;
                                421                 :                :     }
                                422                 :                : 
                                423                 :           2130 :     return result;
                                424                 :                : }
                                425                 :                : 
                                426                 :                : /*
                                427                 :                :  * Derive type information from a relation attribute.
                                428                 :                :  *
                                429                 :                :  * This is needed for setting most slot statistics for all data types.
                                430                 :                :  *
                                431                 :                :  * This duplicates the logic in examine_attribute() but it will not skip the
                                432                 :                :  * attribute if the attstattarget is 0.
                                433                 :                :  *
                                434                 :                :  * This information, retrieved from pg_attribute and pg_type with some
                                435                 :                :  * specific handling for index expressions, is a prerequisite to calling
                                436                 :                :  * any of the other statatt_*() functions.
                                437                 :                :  */
                                438                 :                : void
   80 michael@paquier.xyz       439                 :GNC         777 : statatt_get_type(Oid reloid, AttrNumber attnum,
                                440                 :                :                  Oid *atttypid, int32 *atttypmod,
                                441                 :                :                  char *atttyptype, Oid *atttypcoll,
                                442                 :                :                  Oid *eq_opr, Oid *lt_opr)
                                443                 :                : {
                                444                 :            777 :     Relation    rel = relation_open(reloid, AccessShareLock);
                                445                 :                :     Form_pg_attribute attr;
                                446                 :                :     HeapTuple   atup;
                                447                 :                :     Node       *expr;
                                448                 :                :     TypeCacheEntry *typcache;
                                449                 :                : 
                                450                 :            777 :     atup = SearchSysCache2(ATTNUM, ObjectIdGetDatum(reloid),
                                451                 :                :                            Int16GetDatum(attnum));
                                452                 :                : 
                                453                 :                :     /* Attribute not found */
                                454         [ -  + ]:            777 :     if (!HeapTupleIsValid(atup))
   80 michael@paquier.xyz       455         [ #  # ]:UNC           0 :         ereport(ERROR,
                                456                 :                :                 (errcode(ERRCODE_UNDEFINED_COLUMN),
                                457                 :                :                  errmsg("column %d of relation \"%s\" does not exist",
                                458                 :                :                         attnum, RelationGetRelationName(rel))));
                                459                 :                : 
   80 michael@paquier.xyz       460                 :GNC         777 :     attr = (Form_pg_attribute) GETSTRUCT(atup);
                                461                 :                : 
                                462         [ -  + ]:            777 :     if (attr->attisdropped)
   80 michael@paquier.xyz       463         [ #  # ]:UNC           0 :         ereport(ERROR,
                                464                 :                :                 (errcode(ERRCODE_UNDEFINED_COLUMN),
                                465                 :                :                  errmsg("column %d of relation \"%s\" does not exist",
                                466                 :                :                         attnum, RelationGetRelationName(rel))));
                                467                 :                : 
   80 michael@paquier.xyz       468                 :GNC         777 :     expr = statatt_get_index_expr(rel, attr->attnum);
                                469                 :                : 
                                470                 :                :     /*
                                471                 :                :      * When analyzing an expression index, believe the expression tree's type
                                472                 :                :      * not the column datatype --- the latter might be the opckeytype storage
                                473                 :                :      * type of the opclass, which is not interesting for our purposes.  This
                                474                 :                :      * mimics the behavior of examine_attribute().
                                475                 :                :      */
                                476         [ +  + ]:            777 :     if (expr == NULL)
                                477                 :                :     {
                                478                 :            770 :         *atttypid = attr->atttypid;
                                479                 :            770 :         *atttypmod = attr->atttypmod;
                                480                 :            770 :         *atttypcoll = attr->attcollation;
                                481                 :                :     }
                                482                 :                :     else
                                483                 :                :     {
                                484                 :              7 :         *atttypid = exprType(expr);
                                485                 :              7 :         *atttypmod = exprTypmod(expr);
                                486                 :                : 
                                487         [ -  + ]:              7 :         if (OidIsValid(attr->attcollation))
   80 michael@paquier.xyz       488                 :UNC           0 :             *atttypcoll = attr->attcollation;
                                489                 :                :         else
   80 michael@paquier.xyz       490                 :GNC           7 :             *atttypcoll = exprCollation(expr);
                                491                 :                :     }
                                492                 :            777 :     ReleaseSysCache(atup);
                                493                 :                : 
                                494                 :                :     /*
                                495                 :                :      * If it's a multirange, step down to the range type, as is done by
                                496                 :                :      * multirange_typanalyze().
                                497                 :                :      */
                                498         [ +  + ]:            777 :     if (type_is_multirange(*atttypid))
                                499                 :              4 :         *atttypid = get_multirange_range(*atttypid);
                                500                 :                : 
                                501                 :                :     /* finds the right operators even if atttypid is a domain */
                                502                 :            777 :     typcache = lookup_type_cache(*atttypid, TYPECACHE_LT_OPR | TYPECACHE_EQ_OPR);
                                503                 :            777 :     *atttyptype = typcache->typtype;
                                504                 :            777 :     *eq_opr = typcache->eq_opr;
                                505                 :            777 :     *lt_opr = typcache->lt_opr;
                                506                 :                : 
                                507                 :                :     /*
                                508                 :                :      * Special case: collation for tsvector is DEFAULT_COLLATION_OID. See
                                509                 :                :      * compute_tsvector_stats().
                                510                 :                :      */
                                511         [ +  + ]:            777 :     if (*atttypid == TSVECTOROID)
                                512                 :              1 :         *atttypcoll = DEFAULT_COLLATION_OID;
                                513                 :                : 
                                514                 :            777 :     relation_close(rel, NoLock);
                                515                 :            777 : }
                                516                 :                : 
                                517                 :                : /*
                                518                 :                :  * Derive element type information from the attribute type.  This information
                                519                 :                :  * is needed when the given type is one that contains elements of other types.
                                520                 :                :  *
                                521                 :                :  * The atttypid and atttyptype should be derived from a previous call to
                                522                 :                :  * statatt_get_type().
                                523                 :                :  */
                                524                 :                : bool
                                525                 :             47 : statatt_get_elem_type(Oid atttypid, char atttyptype,
                                526                 :                :                       Oid *elemtypid, Oid *elem_eq_opr)
                                527                 :                : {
                                528                 :                :     TypeCacheEntry *elemtypcache;
                                529                 :                : 
                                530         [ +  + ]:             47 :     if (atttypid == TSVECTOROID)
                                531                 :                :     {
                                532                 :                :         /*
                                533                 :                :          * Special case: element type for tsvector is text. See
                                534                 :                :          * compute_tsvector_stats().
                                535                 :                :          */
                                536                 :              4 :         *elemtypid = TEXTOID;
                                537                 :                :     }
                                538                 :                :     else
                                539                 :                :     {
                                540                 :                :         /* find underlying element type through any domain */
                                541                 :             43 :         *elemtypid = get_base_element_type(atttypid);
                                542                 :                :     }
                                543                 :                : 
                                544         [ +  + ]:             47 :     if (!OidIsValid(*elemtypid))
                                545                 :             15 :         return false;
                                546                 :                : 
                                547                 :                :     /* finds the right operator even if elemtypid is a domain */
                                548                 :             32 :     elemtypcache = lookup_type_cache(*elemtypid, TYPECACHE_EQ_OPR);
                                549         [ -  + ]:             32 :     if (!OidIsValid(elemtypcache->eq_opr))
   80 michael@paquier.xyz       550                 :UNC           0 :         return false;
                                551                 :                : 
   80 michael@paquier.xyz       552                 :GNC          32 :     *elem_eq_opr = elemtypcache->eq_opr;
                                553                 :                : 
                                554                 :             32 :     return true;
                                555                 :                : }
                                556                 :                : 
                                557                 :                : /*
                                558                 :                :  * Build an array with element type elemtypid from a text datum, used as
                                559                 :                :  * value of an attribute in a tuple to-be-inserted into pg_statistic.
                                560                 :                :  *
                                561                 :                :  * The typid and typmod should be derived from a previous call to
                                562                 :                :  * statatt_get_type().
                                563                 :                :  *
                                564                 :                :  * If an error is encountered, capture it and throw a WARNING, with "ok" set
                                565                 :                :  * to false.  If the resulting array contains NULLs, raise a WARNING and
                                566                 :                :  * set "ok" to false.  When the operation succeeds, set "ok" to true.
                                567                 :                :  */
                                568                 :                : Datum
                                569                 :            800 : statatt_build_stavalues(const char *staname, FmgrInfo *array_in, Datum d, Oid typid,
                                570                 :                :                         int32 typmod, bool *ok)
                                571                 :                : {
                                572                 :            800 :     LOCAL_FCINFO(fcinfo, 8);
                                573                 :                :     char       *s;
                                574                 :                :     Datum       result;
                                575                 :            800 :     ErrorSaveContext escontext = {T_ErrorSaveContext};
                                576                 :                : 
                                577                 :            800 :     escontext.details_wanted = true;
                                578                 :                : 
                                579                 :            800 :     s = TextDatumGetCString(d);
                                580                 :                : 
                                581                 :            800 :     InitFunctionCallInfoData(*fcinfo, array_in, 3, InvalidOid,
                                582                 :                :                              (Node *) &escontext, NULL);
                                583                 :                : 
                                584                 :            800 :     fcinfo->args[0].value = CStringGetDatum(s);
                                585                 :            800 :     fcinfo->args[0].isnull = false;
                                586                 :            800 :     fcinfo->args[1].value = ObjectIdGetDatum(typid);
                                587                 :            800 :     fcinfo->args[1].isnull = false;
                                588                 :            800 :     fcinfo->args[2].value = Int32GetDatum(typmod);
                                589                 :            800 :     fcinfo->args[2].isnull = false;
                                590                 :                : 
                                591                 :            800 :     result = FunctionCallInvoke(fcinfo);
                                592                 :                : 
                                593                 :            800 :     pfree(s);
                                594                 :                : 
                                595         [ +  + ]:            800 :     if (escontext.error_occurred)
                                596                 :                :     {
                                597                 :              3 :         escontext.error_data->elevel = WARNING;
                                598                 :              3 :         ThrowErrorData(escontext.error_data);
                                599                 :              3 :         *ok = false;
                                600                 :              3 :         return (Datum) 0;
                                601                 :                :     }
                                602                 :                : 
                                603         [ +  + ]:            797 :     if (array_contains_nulls(DatumGetArrayTypeP(result)))
                                604                 :                :     {
                                605         [ +  - ]:              3 :         ereport(WARNING,
                                606                 :                :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                607                 :                :                  errmsg("\"%s\" array must not contain null values", staname)));
                                608                 :              3 :         *ok = false;
                                609                 :              3 :         return (Datum) 0;
                                610                 :                :     }
                                611                 :                : 
                                612                 :            794 :     *ok = true;
                                613                 :                : 
                                614                 :            794 :     return result;
                                615                 :                : }
                                616                 :                : 
                                617                 :                : /*
                                618                 :                :  * Find and update the slot of a stakind, or use the first empty slot.
                                619                 :                :  *
                                620                 :                :  * Core statistics types expect the stakind value to be one of the
                                621                 :                :  * STATISTIC_KIND_* constants defined in pg_statistic.h, but types defined
                                622                 :                :  * by extensions are not restricted to those values.
                                623                 :                :  *
                                624                 :                :  * In the case of core statistics, the required staop is determined by the
                                625                 :                :  * stakind given and will either be a hardcoded oid, or the eq/lt operator
                                626                 :                :  * derived from statatt_get_type().  Likewise, types defined by extensions
                                627                 :                :  * have no such restriction.
                                628                 :                :  *
                                629                 :                :  * The stacoll value should be either the atttypcoll derived from
                                630                 :                :  * statatt_get_type(), or a hardcoded value required by that particular
                                631                 :                :  * stakind.
                                632                 :                :  *
                                633                 :                :  * The value/null pairs for stanumbers and stavalues should be calculated
                                634                 :                :  * based on the stakind, using statatt_build_stavalues() or constructed arrays.
                                635                 :                :  */
                                636                 :                : void
                                637                 :           1563 : statatt_set_slot(Datum *values, bool *nulls, bool *replaces,
                                638                 :                :                  int16 stakind, Oid staop, Oid stacoll,
                                639                 :                :                  Datum stanumbers, bool stanumbers_isnull,
                                640                 :                :                  Datum stavalues, bool stavalues_isnull)
                                641                 :                : {
                                642                 :                :     int         slotidx;
                                643                 :           1563 :     int         first_empty = -1;
                                644                 :                :     AttrNumber  stakind_attnum;
                                645                 :                :     AttrNumber  staop_attnum;
                                646                 :                :     AttrNumber  stacoll_attnum;
                                647                 :                : 
                                648                 :                :     /* find existing slot with given stakind */
                                649         [ +  + ]:           9378 :     for (slotidx = 0; slotidx < STATISTIC_NUM_SLOTS; slotidx++)
                                650                 :                :     {
                                651                 :           7815 :         stakind_attnum = Anum_pg_statistic_stakind1 - 1 + slotidx;
                                652                 :                : 
                                653   [ +  +  +  + ]:          10355 :         if (first_empty < 0 &&
                                654                 :           2540 :             DatumGetInt16(values[stakind_attnum]) == 0)
                                655                 :           1563 :             first_empty = slotidx;
                                656         [ -  + ]:           7815 :         if (DatumGetInt16(values[stakind_attnum]) == stakind)
   80 michael@paquier.xyz       657                 :UNC           0 :             break;
                                658                 :                :     }
                                659                 :                : 
   80 michael@paquier.xyz       660   [ +  -  +  - ]:GNC        1563 :     if (slotidx >= STATISTIC_NUM_SLOTS && first_empty >= 0)
                                661                 :           1563 :         slotidx = first_empty;
                                662                 :                : 
                                663         [ -  + ]:           1563 :     if (slotidx >= STATISTIC_NUM_SLOTS)
   80 michael@paquier.xyz       664         [ #  # ]:UNC           0 :         ereport(ERROR,
                                665                 :                :                 (errmsg("maximum number of statistics slots exceeded: %d",
                                666                 :                :                         slotidx + 1)));
                                667                 :                : 
   80 michael@paquier.xyz       668                 :GNC        1563 :     stakind_attnum = Anum_pg_statistic_stakind1 - 1 + slotidx;
                                669                 :           1563 :     staop_attnum = Anum_pg_statistic_staop1 - 1 + slotidx;
                                670                 :           1563 :     stacoll_attnum = Anum_pg_statistic_stacoll1 - 1 + slotidx;
                                671                 :                : 
                                672         [ +  - ]:           1563 :     if (DatumGetInt16(values[stakind_attnum]) != stakind)
                                673                 :                :     {
                                674                 :           1563 :         values[stakind_attnum] = Int16GetDatum(stakind);
                                675                 :           1563 :         replaces[stakind_attnum] = true;
                                676                 :                :     }
                                677         [ +  + ]:           1563 :     if (DatumGetObjectId(values[staop_attnum]) != staop)
                                678                 :                :     {
                                679                 :           1544 :         values[staop_attnum] = ObjectIdGetDatum(staop);
                                680                 :           1544 :         replaces[staop_attnum] = true;
                                681                 :                :     }
                                682         [ +  + ]:           1563 :     if (DatumGetObjectId(values[stacoll_attnum]) != stacoll)
                                683                 :                :     {
                                684                 :            351 :         values[stacoll_attnum] = ObjectIdGetDatum(stacoll);
                                685                 :            351 :         replaces[stacoll_attnum] = true;
                                686                 :                :     }
                                687         [ +  + ]:           1563 :     if (!stanumbers_isnull)
                                688                 :                :     {
                                689                 :           1157 :         values[Anum_pg_statistic_stanumbers1 - 1 + slotidx] = stanumbers;
                                690                 :           1157 :         nulls[Anum_pg_statistic_stanumbers1 - 1 + slotidx] = false;
                                691                 :           1157 :         replaces[Anum_pg_statistic_stanumbers1 - 1 + slotidx] = true;
                                692                 :                :     }
                                693         [ +  + ]:           1563 :     if (!stavalues_isnull)
                                694                 :                :     {
                                695                 :            863 :         values[Anum_pg_statistic_stavalues1 - 1 + slotidx] = stavalues;
                                696                 :            863 :         nulls[Anum_pg_statistic_stavalues1 - 1 + slotidx] = false;
                                697                 :            863 :         replaces[Anum_pg_statistic_stavalues1 - 1 + slotidx] = true;
                                698                 :                :     }
                                699                 :           1563 : }
                                700                 :                : 
                                701                 :                : /*
                                702                 :                :  * Initialize values and nulls for a new pg_statistic tuple.
                                703                 :                :  *
                                704                 :                :  * The caller is responsible for allocating the arrays where the results are
                                705                 :                :  * stored, which should be of size Natts_pg_statistic.
                                706                 :                :  *
                                707                 :                :  * When using this routine for a tuple inserted into pg_statistic, reloid,
                                708                 :                :  * attnum and inherited flags should all be set.
                                709                 :                :  *
                                710                 :                :  * When using this routine for a tuple that is an element of a stxdexpr
                                711                 :                :  * array inserted into pg_statistic_ext_data, reloid, attnum and inherited
                                712                 :                :  * should be respectively set to InvalidOid, InvalidAttrNumber and false.
                                713                 :                :  */
                                714                 :                : void
                                715                 :            825 : statatt_init_empty_tuple(Oid reloid, int16 attnum, bool inherited,
                                716                 :                :                          Datum *values, bool *nulls, bool *replaces)
                                717                 :                : {
                                718                 :            825 :     memset(nulls, true, sizeof(bool) * Natts_pg_statistic);
                                719                 :            825 :     memset(replaces, true, sizeof(bool) * Natts_pg_statistic);
                                720                 :                : 
                                721                 :                :     /* This must initialize non-NULL attributes */
                                722                 :            825 :     values[Anum_pg_statistic_starelid - 1] = ObjectIdGetDatum(reloid);
                                723                 :            825 :     nulls[Anum_pg_statistic_starelid - 1] = false;
                                724                 :            825 :     values[Anum_pg_statistic_staattnum - 1] = Int16GetDatum(attnum);
                                725                 :            825 :     nulls[Anum_pg_statistic_staattnum - 1] = false;
                                726                 :            825 :     values[Anum_pg_statistic_stainherit - 1] = BoolGetDatum(inherited);
                                727                 :            825 :     nulls[Anum_pg_statistic_stainherit - 1] = false;
                                728                 :                : 
                                729                 :            825 :     values[Anum_pg_statistic_stanullfrac - 1] = DEFAULT_STATATT_NULL_FRAC;
                                730                 :            825 :     nulls[Anum_pg_statistic_stanullfrac - 1] = false;
                                731                 :            825 :     values[Anum_pg_statistic_stawidth - 1] = DEFAULT_STATATT_AVG_WIDTH;
                                732                 :            825 :     nulls[Anum_pg_statistic_stawidth - 1] = false;
                                733                 :            825 :     values[Anum_pg_statistic_stadistinct - 1] = DEFAULT_STATATT_N_DISTINCT;
                                734                 :            825 :     nulls[Anum_pg_statistic_stadistinct - 1] = false;
                                735                 :                : 
                                736                 :                :     /* initialize stakind, staop, and stacoll slots */
                                737         [ +  + ]:           4950 :     for (int slotnum = 0; slotnum < STATISTIC_NUM_SLOTS; slotnum++)
                                738                 :                :     {
                                739                 :           4125 :         values[Anum_pg_statistic_stakind1 + slotnum - 1] = (Datum) 0;
                                740                 :           4125 :         nulls[Anum_pg_statistic_stakind1 + slotnum - 1] = false;
                                741                 :           4125 :         values[Anum_pg_statistic_staop1 + slotnum - 1] = ObjectIdGetDatum(InvalidOid);
                                742                 :           4125 :         nulls[Anum_pg_statistic_staop1 + slotnum - 1] = false;
                                743                 :           4125 :         values[Anum_pg_statistic_stacoll1 + slotnum - 1] = ObjectIdGetDatum(InvalidOid);
                                744                 :           4125 :         nulls[Anum_pg_statistic_stacoll1 + slotnum - 1] = false;
                                745                 :                :     }
                                746                 :            825 : }
        

Generated by: LCOV version 2.4-beta