LCOV - differential code coverage report
Current view: top level - src/backend/statistics - attribute_stats.c (source / functions) Coverage Total Hit UNC UBC GBC GNC CBC DCB
Current: 77aeca80249c9e640c811e80633a2e334a9320de vs 38afc3dcb25c45b744d4025029ce0a6c90b7059f Lines: 93.7 % 255 239 16 1 2 236 1
Current Date: 2026-07-25 19:08:27 +0900 Functions: 100.0 % 8 8 1 7
Baseline: lcov-20260725-baseline Branches: 71.2 % 170 121 1 48 3 118
Baseline Date: 2026-07-25 19:09:19 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 98.6 % 73 72 1 2 70
(30,360] days: 100.0 % 27 27 27
(360..) days: 90.3 % 155 140 15 1 139
Function coverage date bins:
(7,30] days: 100.0 % 3 3 1 2
(360..) days: 100.0 % 5 5 5
Branch coverage date bins:
(7,30] days: 62.5 % 48 30 1 17 3 27
(30,360] days: 83.3 % 6 5 1 5
(360..) days: 74.1 % 116 86 30 86

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  * attribute_stats.c
                                  3                 :                :  *
                                  4                 :                :  *    PostgreSQL relation attribute statistics manipulation.
                                  5                 :                :  *
                                  6                 :                :  * Code supporting the direct import of relation attribute statistics, similar
                                  7                 :                :  * to what is done by the ANALYZE command.
                                  8                 :                :  *
                                  9                 :                :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
                                 10                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                 11                 :                :  *
                                 12                 :                :  * IDENTIFICATION
                                 13                 :                :  *       src/backend/statistics/attribute_stats.c
                                 14                 :                :  *
                                 15                 :                :  *-------------------------------------------------------------------------
                                 16                 :                :  */
                                 17                 :                : 
                                 18                 :                : #include "postgres.h"
                                 19                 :                : 
                                 20                 :                : #include "access/heapam.h"
                                 21                 :                : #include "catalog/indexing.h"
                                 22                 :                : #include "catalog/namespace.h"
                                 23                 :                : #include "catalog/pg_operator.h"
                                 24                 :                : #include "nodes/makefuncs.h"
                                 25                 :                : #include "statistics/statistics.h"
                                 26                 :                : #include "statistics/stat_utils.h"
                                 27                 :                : #include "utils/array.h"
                                 28                 :                : #include "utils/builtins.h"
                                 29                 :                : #include "utils/fmgroids.h"
                                 30                 :                : #include "utils/lsyscache.h"
                                 31                 :                : #include "utils/syscache.h"
                                 32                 :                : 
                                 33                 :                : /*
                                 34                 :                :  * Positional argument numbers, names, and types for
                                 35                 :                :  * attribute_statistics_update() and pg_restore_attribute_stats().
                                 36                 :                :  */
                                 37                 :                : 
                                 38                 :                : enum attribute_stats_argnum
                                 39                 :                : {
                                 40                 :                :     ATTRELSCHEMA_ARG = 0,
                                 41                 :                :     ATTRELNAME_ARG,
                                 42                 :                :     ATTNAME_ARG,
                                 43                 :                :     ATTNUM_ARG,
                                 44                 :                :     INHERITED_ARG,
                                 45                 :                :     NULL_FRAC_ARG,
                                 46                 :                :     AVG_WIDTH_ARG,
                                 47                 :                :     N_DISTINCT_ARG,
                                 48                 :                :     MOST_COMMON_VALS_ARG,
                                 49                 :                :     MOST_COMMON_FREQS_ARG,
                                 50                 :                :     HISTOGRAM_BOUNDS_ARG,
                                 51                 :                :     CORRELATION_ARG,
                                 52                 :                :     MOST_COMMON_ELEMS_ARG,
                                 53                 :                :     MOST_COMMON_ELEM_FREQS_ARG,
                                 54                 :                :     ELEM_COUNT_HISTOGRAM_ARG,
                                 55                 :                :     RANGE_LENGTH_HISTOGRAM_ARG,
                                 56                 :                :     RANGE_EMPTY_FRAC_ARG,
                                 57                 :                :     RANGE_BOUNDS_HISTOGRAM_ARG,
                                 58                 :                :     NUM_ATTRIBUTE_STATS_ARGS
                                 59                 :                : };
                                 60                 :                : 
                                 61                 :                : static struct StatsArgInfo attarginfo[] =
                                 62                 :                : {
                                 63                 :                :     [ATTRELSCHEMA_ARG] = {"schemaname", TEXTOID},
                                 64                 :                :     [ATTRELNAME_ARG] = {"relname", TEXTOID},
                                 65                 :                :     [ATTNAME_ARG] = {"attname", TEXTOID},
                                 66                 :                :     [ATTNUM_ARG] = {"attnum", INT2OID},
                                 67                 :                :     [INHERITED_ARG] = {"inherited", BOOLOID},
                                 68                 :                :     [NULL_FRAC_ARG] = {"null_frac", FLOAT4OID},
                                 69                 :                :     [AVG_WIDTH_ARG] = {"avg_width", INT4OID},
                                 70                 :                :     [N_DISTINCT_ARG] = {"n_distinct", FLOAT4OID},
                                 71                 :                :     [MOST_COMMON_VALS_ARG] = {"most_common_vals", TEXTOID},
                                 72                 :                :     [MOST_COMMON_FREQS_ARG] = {"most_common_freqs", FLOAT4ARRAYOID},
                                 73                 :                :     [HISTOGRAM_BOUNDS_ARG] = {"histogram_bounds", TEXTOID},
                                 74                 :                :     [CORRELATION_ARG] = {"correlation", FLOAT4OID},
                                 75                 :                :     [MOST_COMMON_ELEMS_ARG] = {"most_common_elems", TEXTOID},
                                 76                 :                :     [MOST_COMMON_ELEM_FREQS_ARG] = {"most_common_elem_freqs", FLOAT4ARRAYOID},
                                 77                 :                :     [ELEM_COUNT_HISTOGRAM_ARG] = {"elem_count_histogram", FLOAT4ARRAYOID},
                                 78                 :                :     [RANGE_LENGTH_HISTOGRAM_ARG] = {"range_length_histogram", TEXTOID},
                                 79                 :                :     [RANGE_EMPTY_FRAC_ARG] = {"range_empty_frac", FLOAT4OID},
                                 80                 :                :     [RANGE_BOUNDS_HISTOGRAM_ARG] = {"range_bounds_histogram", TEXTOID},
                                 81                 :                :     [NUM_ATTRIBUTE_STATS_ARGS] = {0}
                                 82                 :                : };
                                 83                 :                : 
                                 84                 :                : /*
                                 85                 :                :  * Positional argument numbers, names, and types for
                                 86                 :                :  * pg_clear_attribute_stats().
                                 87                 :                :  */
                                 88                 :                : 
                                 89                 :                : enum clear_attribute_stats_argnum
                                 90                 :                : {
                                 91                 :                :     C_ATTRELSCHEMA_ARG = 0,
                                 92                 :                :     C_ATTRELNAME_ARG,
                                 93                 :                :     C_ATTNAME_ARG,
                                 94                 :                :     C_INHERITED_ARG,
                                 95                 :                :     C_NUM_ATTRIBUTE_STATS_ARGS
                                 96                 :                : };
                                 97                 :                : 
                                 98                 :                : static struct StatsArgInfo cleararginfo[] =
                                 99                 :                : {
                                100                 :                :     [C_ATTRELSCHEMA_ARG] = {"schemaname", TEXTOID},
                                101                 :                :     [C_ATTRELNAME_ARG] = {"relname", TEXTOID},
                                102                 :                :     [C_ATTNAME_ARG] = {"attname", TEXTOID},
                                103                 :                :     [C_INHERITED_ARG] = {"inherited", BOOLOID},
                                104                 :                :     [C_NUM_ATTRIBUTE_STATS_ARGS] = {0}
                                105                 :                : };
                                106                 :                : 
                                107                 :                : static bool attribute_statistics_update(FunctionCallInfo fcinfo);
                                108                 :                : static bool attribute_statistics_update_internal(Oid reloid,
                                109                 :                :                                                  const char *attname,
                                110                 :                :                                                  AttrNumber attnum,
                                111                 :                :                                                  bool inherited,
                                112                 :                :                                                  FunctionCallInfo fcinfo);
                                113                 :                : static void upsert_pg_statistic(Relation starel, HeapTuple oldtup,
                                114                 :                :                                 const Datum *values, const bool *nulls, const bool *replaces);
                                115                 :                : static bool delete_pg_statistic(Oid reloid, AttrNumber attnum, bool stainherit);
                                116                 :                : 
                                117                 :                : /*
                                118                 :                :  * Insert or Update Attribute Statistics
                                119                 :                :  *
                                120                 :                :  * See pg_statistic.h for an explanation of how each statistic kind is
                                121                 :                :  * stored. Custom statistics kinds are not supported.
                                122                 :                :  *
                                123                 :                :  * Depending on the statistics kind, we need to derive information from the
                                124                 :                :  * attribute for which we're storing the stats. For instance, the MCVs are
                                125                 :                :  * stored as an anyarray, and the representation of the array needs to store
                                126                 :                :  * the correct element type, which must be derived from the attribute.
                                127                 :                :  *
                                128                 :                :  * Major errors, such as the table not existing, the attribute not existing,
                                129                 :                :  * or a permissions failure are always reported at ERROR. Other errors, such
                                130                 :                :  * as a conversion failure on one statistic kind, are reported as a WARNING
                                131                 :                :  * and other statistic kinds may still be updated.
                                132                 :                :  */
                                133                 :                : static bool
  515 jdavis@postgresql.or      134                 :CBC         874 : attribute_statistics_update(FunctionCallInfo fcinfo)
                                135                 :                : {
                                136                 :                :     char       *nspname;
                                137                 :                :     char       *relname;
                                138                 :                :     Oid         reloid;
                                139                 :                :     char       *attname;
                                140                 :                :     AttrNumber  attnum;
                                141                 :                :     bool        inherited;
  283 nathan@postgresql.or      142                 :            874 :     Oid         locked_table = InvalidOid;
                                143                 :                : 
  487 jdavis@postgresql.or      144                 :            874 :     stats_check_required_arg(fcinfo, attarginfo, ATTRELSCHEMA_ARG);
                                145                 :            870 :     stats_check_required_arg(fcinfo, attarginfo, ATTRELNAME_ARG);
                                146                 :                : 
                                147                 :            862 :     nspname = TextDatumGetCString(PG_GETARG_DATUM(ATTRELSCHEMA_ARG));
                                148                 :            862 :     relname = TextDatumGetCString(PG_GETARG_DATUM(ATTRELNAME_ARG));
                                149                 :                : 
  612 fujii@postgresql.org      150         [ -  + ]:            862 :     if (RecoveryInProgress())
  612 fujii@postgresql.org      151         [ #  # ]:UBC           0 :         ereport(ERROR,
                                152                 :                :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                                153                 :                :                  errmsg("recovery is in progress"),
                                154                 :                :                  errhint("Statistics cannot be modified during recovery.")));
                                155                 :                : 
                                156                 :                :     /* lock before looking up attribute */
  283 nathan@postgresql.or      157                 :CBC         862 :     reloid = RangeVarGetRelidExtended(makeRangeVar(nspname, relname, -1),
                                158                 :                :                                       ShareUpdateExclusiveLock, 0,
                                159                 :                :                                       RangeVarCallbackForStats, &locked_table);
                                160                 :                : 
                                161                 :                :     /* user can specify either attname or attnum, but not both */
  514 tgl@sss.pgh.pa.us         162         [ +  + ]:            854 :     if (!PG_ARGISNULL(ATTNAME_ARG))
                                163                 :                :     {
                                164         [ +  + ]:            838 :         if (!PG_ARGISNULL(ATTNUM_ARG))
                                165         [ +  - ]:              4 :             ereport(ERROR,
                                166                 :                :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                167                 :                :                      errmsg("cannot specify both \"%s\" and \"%s\"", "attname", "attnum")));
  487 jdavis@postgresql.or      168                 :            834 :         attname = TextDatumGetCString(PG_GETARG_DATUM(ATTNAME_ARG));
  514 tgl@sss.pgh.pa.us         169                 :            834 :         attnum = get_attnum(reloid, attname);
                                170                 :                :         /* note that this test covers attisdropped cases too: */
                                171         [ +  + ]:            834 :         if (attnum == InvalidAttrNumber)
                                172         [ +  - ]:              4 :             ereport(ERROR,
                                173                 :                :                     (errcode(ERRCODE_UNDEFINED_COLUMN),
                                174                 :                :                      errmsg("column \"%s\" of relation \"%s\" does not exist",
                                175                 :                :                             attname, relname)));
                                176                 :                :     }
                                177         [ +  + ]:             16 :     else if (!PG_ARGISNULL(ATTNUM_ARG))
                                178                 :                :     {
                                179                 :              8 :         attnum = PG_GETARG_INT16(ATTNUM_ARG);
                                180                 :              8 :         attname = get_attname(reloid, attnum, true);
                                181                 :                :         /* annoyingly, get_attname doesn't check attisdropped */
                                182         [ +  - ]:              8 :         if (attname == NULL ||
                                183         [ -  + ]:              8 :             !SearchSysCacheExistsAttName(reloid, attname))
  514 tgl@sss.pgh.pa.us         184         [ #  # ]:UBC           0 :             ereport(ERROR,
                                185                 :                :                     (errcode(ERRCODE_UNDEFINED_COLUMN),
                                186                 :                :                      errmsg("column %d of relation \"%s\" does not exist",
                                187                 :                :                             attnum, relname)));
                                188                 :                :     }
                                189                 :                :     else
                                190                 :                :     {
  514 tgl@sss.pgh.pa.us         191         [ +  - ]:CBC           8 :         ereport(ERROR,
                                192                 :                :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                193                 :                :                  errmsg("must specify either \"%s\" or \"%s\"", "attname", "attnum")));
                                194                 :                :         attname = NULL;         /* keep compiler quiet */
                                195                 :                :         attnum = 0;
                                196                 :                :     }
                                197                 :                : 
  610 jdavis@postgresql.or      198         [ +  + ]:            838 :     if (attnum < 0)
                                199         [ +  - ]:              4 :         ereport(ERROR,
                                200                 :                :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                201                 :                :                  errmsg("cannot modify statistics on system column \"%s\"",
                                202                 :                :                         attname)));
                                203                 :                : 
  641                           204                 :            834 :     stats_check_required_arg(fcinfo, attarginfo, INHERITED_ARG);
                                205                 :            830 :     inherited = PG_GETARG_BOOL(INHERITED_ARG);
                                206                 :                : 
   15 efujita@postgresql.o      207                 :            830 :     return attribute_statistics_update_internal(reloid, attname, attnum,
                                208                 :                :                                                 inherited, fcinfo);
                                209                 :                : }
                                210                 :                : 
                                211                 :                : /*
                                212                 :                :  * Workhorse function for attribute_statistics_update.
                                213                 :                :  */
                                214                 :                : static bool
                                215                 :            841 : attribute_statistics_update_internal(Oid reloid,
                                216                 :                :                                      const char *attname, AttrNumber attnum,
                                217                 :                :                                      bool inherited, FunctionCallInfo fcinfo)
                                218                 :                : {
                                219                 :                :     Relation    starel;
                                220                 :                :     HeapTuple   statup;
                                221                 :                : 
                                222                 :            841 :     Oid         atttypid = InvalidOid;
                                223                 :                :     int32       atttypmod;
                                224                 :                :     char        atttyptype;
                                225                 :            841 :     Oid         atttypcoll = InvalidOid;
                                226                 :            841 :     Oid         eq_opr = InvalidOid;
                                227                 :            841 :     Oid         lt_opr = InvalidOid;
                                228                 :                : 
                                229                 :            841 :     Oid         elemtypid = InvalidOid;
                                230                 :            841 :     Oid         elem_eq_opr = InvalidOid;
                                231                 :                : 
                                232                 :                :     FmgrInfo    array_in_fn;
                                233                 :                : 
                                234         [ +  + ]:           1252 :     bool        do_mcv = !PG_ARGISNULL(MOST_COMMON_FREQS_ARG) &&
                                235         [ +  + ]:            411 :         !PG_ARGISNULL(MOST_COMMON_VALS_ARG);
                                236                 :            841 :     bool        do_histogram = !PG_ARGISNULL(HISTOGRAM_BOUNDS_ARG);
                                237                 :            841 :     bool        do_correlation = !PG_ARGISNULL(CORRELATION_ARG);
                                238         [ +  + ]:            869 :     bool        do_mcelem = !PG_ARGISNULL(MOST_COMMON_ELEMS_ARG) &&
                                239         [ +  + ]:             28 :         !PG_ARGISNULL(MOST_COMMON_ELEM_FREQS_ARG);
                                240                 :            841 :     bool        do_dechist = !PG_ARGISNULL(ELEM_COUNT_HISTOGRAM_ARG);
                                241                 :            841 :     bool        do_bounds_histogram = !PG_ARGISNULL(RANGE_BOUNDS_HISTOGRAM_ARG);
                                242         [ +  + ]:            865 :     bool        do_range_length_histogram = !PG_ARGISNULL(RANGE_LENGTH_HISTOGRAM_ARG) &&
                                243         [ +  + ]:             24 :         !PG_ARGISNULL(RANGE_EMPTY_FRAC_ARG);
                                244                 :                : 
                                245                 :            841 :     Datum       values[Natts_pg_statistic] = {0};
                                246                 :            841 :     bool        nulls[Natts_pg_statistic] = {0};
                                247                 :            841 :     bool        replaces[Natts_pg_statistic] = {0};
                                248                 :                : 
                                249                 :            841 :     bool        result = true;
                                250                 :                : 
                                251                 :                :     /*
                                252                 :                :      * Check argument sanity. If some arguments are unusable, emit a WARNING
                                253                 :                :      * and set the corresponding argument to NULL in fcinfo.
                                254                 :                :      */
                                255                 :                : 
  515 jdavis@postgresql.or      256         [ -  + ]:            841 :     if (!stats_check_arg_array(fcinfo, attarginfo, MOST_COMMON_FREQS_ARG))
                                257                 :                :     {
  641 jdavis@postgresql.or      258                 :UBC           0 :         do_mcv = false;
                                259                 :              0 :         result = false;
                                260                 :                :     }
                                261                 :                : 
  515 jdavis@postgresql.or      262         [ -  + ]:CBC         841 :     if (!stats_check_arg_array(fcinfo, attarginfo, MOST_COMMON_ELEM_FREQS_ARG))
                                263                 :                :     {
  641 jdavis@postgresql.or      264                 :UBC           0 :         do_mcelem = false;
                                265                 :              0 :         result = false;
                                266                 :                :     }
  515 jdavis@postgresql.or      267         [ +  + ]:CBC         841 :     if (!stats_check_arg_array(fcinfo, attarginfo, ELEM_COUNT_HISTOGRAM_ARG))
                                268                 :                :     {
  641                           269                 :              4 :         do_dechist = false;
                                270                 :              4 :         result = false;
                                271                 :                :     }
                                272                 :                : 
                                273         [ +  + ]:            841 :     if (!stats_check_arg_pair(fcinfo, attarginfo,
                                274                 :                :                               MOST_COMMON_VALS_ARG, MOST_COMMON_FREQS_ARG))
                                275                 :                :     {
                                276                 :             12 :         do_mcv = false;
                                277                 :             12 :         result = false;
                                278                 :                :     }
                                279                 :                : 
                                280         [ +  + ]:            841 :     if (!stats_check_arg_pair(fcinfo, attarginfo,
                                281                 :                :                               MOST_COMMON_ELEMS_ARG,
                                282                 :                :                               MOST_COMMON_ELEM_FREQS_ARG))
                                283                 :                :     {
                                284                 :              8 :         do_mcelem = false;
                                285                 :              8 :         result = false;
                                286                 :                :     }
                                287                 :                : 
                                288         [ +  + ]:            841 :     if (!stats_check_arg_pair(fcinfo, attarginfo,
                                289                 :                :                               RANGE_LENGTH_HISTOGRAM_ARG,
                                290                 :                :                               RANGE_EMPTY_FRAC_ARG))
                                291                 :                :     {
                                292                 :              8 :         do_range_length_histogram = false;
                                293                 :              8 :         result = false;
                                294                 :                :     }
                                295                 :                : 
                                296                 :                :     /* derive information from attribute */
  212 michael@paquier.xyz       297                 :            841 :     statatt_get_type(reloid, attnum,
                                298                 :                :                      &atttypid, &atttypmod,
                                299                 :                :                      &atttyptype, &atttypcoll,
                                300                 :                :                      &eq_opr, &lt_opr);
                                301                 :                : 
                                302                 :                :     /* if needed, derive element type */
  641 jdavis@postgresql.or      303   [ +  +  +  + ]:            841 :     if (do_mcelem || do_dechist)
                                304                 :                :     {
  212 michael@paquier.xyz       305         [ +  + ]:             32 :         if (!statatt_get_elem_type(atttypid, atttyptype,
                                306                 :                :                                    &elemtypid, &elem_eq_opr))
                                307                 :                :         {
  515 jdavis@postgresql.or      308         [ +  - ]:             12 :             ereport(WARNING,
                                309                 :                :                     (errmsg("could not determine element type of column \"%s\"", attname),
                                310                 :                :                      errdetail("Cannot set %s or %s.",
                                311                 :                :                                "STATISTIC_KIND_MCELEM", "STATISTIC_KIND_DECHIST")));
  641                           312                 :             12 :             elemtypid = InvalidOid;
                                313                 :             12 :             elem_eq_opr = InvalidOid;
                                314                 :                : 
                                315                 :             12 :             do_mcelem = false;
                                316                 :             12 :             do_dechist = false;
                                317                 :             12 :             result = false;
                                318                 :                :         }
                                319                 :                :     }
                                320                 :                : 
                                321                 :                :     /* histogram and correlation require less-than operator */
                                322   [ +  +  +  +  :            841 :     if ((do_histogram || do_correlation) && !OidIsValid(lt_opr))
                                              -  + ]
                                323                 :                :     {
  515 jdavis@postgresql.or      324         [ #  # ]:UBC           0 :         ereport(WARNING,
                                325                 :                :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                326                 :                :                  errmsg("could not determine less-than operator for column \"%s\"", attname),
                                327                 :                :                  errdetail("Cannot set %s or %s.",
                                328                 :                :                            "STATISTIC_KIND_HISTOGRAM", "STATISTIC_KIND_CORRELATION")));
                                329                 :                : 
  641                           330                 :              0 :         do_histogram = false;
                                331                 :              0 :         do_correlation = false;
                                332                 :              0 :         result = false;
                                333                 :                :     }
                                334                 :                : 
                                335                 :                :     /* only range types can have range stats */
  641 jdavis@postgresql.or      336   [ +  +  +  + ]:CBC         841 :     if ((do_range_length_histogram || do_bounds_histogram) &&
                                337   [ +  +  +  - ]:             36 :         !(atttyptype == TYPTYPE_RANGE || atttyptype == TYPTYPE_MULTIRANGE))
                                338                 :                :     {
  515                           339         [ +  - ]:              8 :         ereport(WARNING,
                                340                 :                :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                341                 :                :                  errmsg("column \"%s\" is not a range type", attname),
                                342                 :                :                  errdetail("Cannot set %s or %s.",
                                343                 :                :                            "STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM", "STATISTIC_KIND_BOUNDS_HISTOGRAM")));
                                344                 :                : 
  641                           345                 :              8 :         do_bounds_histogram = false;
                                346                 :              8 :         do_range_length_histogram = false;
                                347                 :              8 :         result = false;
                                348                 :                :     }
                                349                 :                : 
                                350                 :            841 :     fmgr_info(F_ARRAY_IN, &array_in_fn);
                                351                 :                : 
                                352                 :            841 :     starel = table_open(StatisticRelationId, RowExclusiveLock);
                                353                 :                : 
  351 peter@eisentraut.org      354                 :            841 :     statup = SearchSysCache3(STATRELATTINH, ObjectIdGetDatum(reloid), Int16GetDatum(attnum), BoolGetDatum(inherited));
                                355                 :                : 
                                356                 :                :     /* initialize from existing tuple if exists */
  641 jdavis@postgresql.or      357         [ +  + ]:            841 :     if (HeapTupleIsValid(statup))
                                358                 :            104 :         heap_deform_tuple(statup, RelationGetDescr(starel), values, nulls);
                                359                 :                :     else
  212 michael@paquier.xyz       360                 :            737 :         statatt_init_empty_tuple(reloid, attnum, inherited, values, nulls,
                                361                 :                :                                  replaces);
                                362                 :                : 
                                363                 :                :     /* if specified, set to argument values */
  641 jdavis@postgresql.or      364         [ +  + ]:            841 :     if (!PG_ARGISNULL(NULL_FRAC_ARG))
                                365                 :                :     {
                                366                 :            809 :         values[Anum_pg_statistic_stanullfrac - 1] = PG_GETARG_DATUM(NULL_FRAC_ARG);
                                367                 :            809 :         replaces[Anum_pg_statistic_stanullfrac - 1] = true;
                                368                 :                :     }
                                369         [ +  + ]:            841 :     if (!PG_ARGISNULL(AVG_WIDTH_ARG))
                                370                 :                :     {
                                371                 :            721 :         values[Anum_pg_statistic_stawidth - 1] = PG_GETARG_DATUM(AVG_WIDTH_ARG);
                                372                 :            721 :         replaces[Anum_pg_statistic_stawidth - 1] = true;
                                373                 :                :     }
                                374         [ +  + ]:            841 :     if (!PG_ARGISNULL(N_DISTINCT_ARG))
                                375                 :                :     {
                                376                 :            721 :         values[Anum_pg_statistic_stadistinct - 1] = PG_GETARG_DATUM(N_DISTINCT_ARG);
                                377                 :            721 :         replaces[Anum_pg_statistic_stadistinct - 1] = true;
                                378                 :                :     }
                                379                 :                : 
                                380                 :                :     /* STATISTIC_KIND_MCV */
                                381         [ +  + ]:            841 :     if (do_mcv)
                                382                 :                :     {
                                383                 :                :         bool        converted;
                                384                 :            407 :         Datum       stanumbers = PG_GETARG_DATUM(MOST_COMMON_FREQS_ARG);
  212 michael@paquier.xyz       385                 :            407 :         Datum       stavalues = statatt_build_stavalues("most_common_vals",
                                386                 :                :                                                         &array_in_fn,
                                387                 :                :                                                         PG_GETARG_DATUM(MOST_COMMON_VALS_ARG),
                                388                 :                :                                                         atttypid, atttypmod,
                                389                 :                :                                                         &converted);
                                390                 :                : 
  641 jdavis@postgresql.or      391         [ +  + ]:            407 :         if (converted)
                                392                 :                :         {
   75 michael@paquier.xyz       393                 :            399 :             ArrayType  *vals_arr = DatumGetArrayTypeP(stavalues);
                                394                 :            399 :             ArrayType  *nums_arr = DatumGetArrayTypeP(stanumbers);
                                395                 :            399 :             int         nvals = ARR_DIMS(vals_arr)[0];
                                396                 :            399 :             int         nnums = ARR_DIMS(nums_arr)[0];
                                397                 :                : 
                                398         [ +  + ]:            399 :             if (nvals != nnums)
                                399                 :                :             {
                                400         [ +  - ]:              8 :                 ereport(WARNING,
                                401                 :                :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                402                 :                :                          errmsg("could not parse \"%s\": incorrect number of elements (same as \"%s\" required)",
                                403                 :                :                                 "most_common_vals",
                                404                 :                :                                 "most_common_freqs")));
                                405                 :              8 :                 result = false;
                                406                 :                :             }
                                407                 :                :             else
                                408                 :                :             {
                                409                 :            391 :                 statatt_set_slot(values, nulls, replaces,
                                410                 :                :                                  STATISTIC_KIND_MCV,
                                411                 :                :                                  eq_opr, atttypcoll,
                                412                 :                :                                  stanumbers, false, stavalues, false);
                                413                 :                :             }
                                414                 :                :         }
                                415                 :                :         else
  641 jdavis@postgresql.or      416                 :              8 :             result = false;
                                417                 :                :     }
                                418                 :                : 
                                419                 :                :     /* STATISTIC_KIND_HISTOGRAM */
                                420         [ +  + ]:            841 :     if (do_histogram)
                                421                 :                :     {
                                422                 :                :         Datum       stavalues;
                                423                 :            388 :         bool        converted = false;
                                424                 :                : 
  212 michael@paquier.xyz       425                 :            388 :         stavalues = statatt_build_stavalues("histogram_bounds",
                                426                 :                :                                             &array_in_fn,
                                427                 :                :                                             PG_GETARG_DATUM(HISTOGRAM_BOUNDS_ARG),
                                428                 :                :                                             atttypid, atttypmod,
                                429                 :                :                                             &converted);
                                430                 :                : 
  641 jdavis@postgresql.or      431         [ +  + ]:            388 :         if (converted)
                                432                 :                :         {
  212 michael@paquier.xyz       433                 :            384 :             statatt_set_slot(values, nulls, replaces,
                                434                 :                :                              STATISTIC_KIND_HISTOGRAM,
                                435                 :                :                              lt_opr, atttypcoll,
                                436                 :                :                              0, true, stavalues, false);
                                437                 :                :         }
                                438                 :                :         else
  641 jdavis@postgresql.or      439                 :              4 :             result = false;
                                440                 :                :     }
                                441                 :                : 
                                442                 :                :     /* STATISTIC_KIND_CORRELATION */
                                443         [ +  + ]:            841 :     if (do_correlation)
                                444                 :                :     {
                                445                 :            671 :         Datum       elems[] = {PG_GETARG_DATUM(CORRELATION_ARG)};
                                446                 :            671 :         ArrayType  *arry = construct_array_builtin(elems, 1, FLOAT4OID);
                                447                 :            671 :         Datum       stanumbers = PointerGetDatum(arry);
                                448                 :                : 
  212 michael@paquier.xyz       449                 :            671 :         statatt_set_slot(values, nulls, replaces,
                                450                 :                :                          STATISTIC_KIND_CORRELATION,
                                451                 :                :                          lt_opr, atttypcoll,
                                452                 :                :                          stanumbers, false, 0, true);
                                453                 :                :     }
                                454                 :                : 
                                455                 :                :     /* STATISTIC_KIND_MCELEM */
  641 jdavis@postgresql.or      456         [ +  + ]:            841 :     if (do_mcelem)
                                457                 :                :     {
                                458                 :             16 :         Datum       stanumbers = PG_GETARG_DATUM(MOST_COMMON_ELEM_FREQS_ARG);
                                459                 :             16 :         bool        converted = false;
                                460                 :                :         Datum       stavalues;
                                461                 :                : 
  212 michael@paquier.xyz       462                 :             16 :         stavalues = statatt_build_stavalues("most_common_elems",
                                463                 :                :                                             &array_in_fn,
                                464                 :                :                                             PG_GETARG_DATUM(MOST_COMMON_ELEMS_ARG),
                                465                 :                :                                             elemtypid, atttypmod,
                                466                 :                :                                             &converted);
                                467                 :                : 
  641 jdavis@postgresql.or      468         [ +  - ]:             16 :         if (converted)
                                469                 :                :         {
  212 michael@paquier.xyz       470                 :             16 :             statatt_set_slot(values, nulls, replaces,
                                471                 :                :                              STATISTIC_KIND_MCELEM,
                                472                 :                :                              elem_eq_opr, atttypcoll,
                                473                 :                :                              stanumbers, false, stavalues, false);
                                474                 :                :         }
                                475                 :                :         else
  641 jdavis@postgresql.or      476                 :UBC           0 :             result = false;
                                477                 :                :     }
                                478                 :                : 
                                479                 :                :     /* STATISTIC_KIND_DECHIST */
  641 jdavis@postgresql.or      480         [ +  + ]:CBC         841 :     if (do_dechist)
                                481                 :                :     {
                                482                 :             15 :         Datum       stanumbers = PG_GETARG_DATUM(ELEM_COUNT_HISTOGRAM_ARG);
                                483                 :                : 
  212 michael@paquier.xyz       484                 :             15 :         statatt_set_slot(values, nulls, replaces,
                                485                 :                :                          STATISTIC_KIND_DECHIST,
                                486                 :                :                          elem_eq_opr, atttypcoll,
                                487                 :                :                          stanumbers, false, 0, true);
                                488                 :                :     }
                                489                 :                : 
                                490                 :                :     /*
                                491                 :                :      * STATISTIC_KIND_BOUNDS_HISTOGRAM
                                492                 :                :      *
                                493                 :                :      * This stakind appears before STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM even
                                494                 :                :      * though it is numerically greater, and all other stakinds appear in
                                495                 :                :      * numerical order. We duplicate this quirk for consistency.
                                496                 :                :      */
  641 jdavis@postgresql.or      497         [ +  + ]:            841 :     if (do_bounds_histogram)
                                498                 :                :     {
                                499                 :             24 :         bool        converted = false;
                                500                 :                :         Datum       stavalues;
                                501                 :                : 
  212 michael@paquier.xyz       502                 :             24 :         stavalues = statatt_build_stavalues("range_bounds_histogram",
                                503                 :                :                                             &array_in_fn,
                                504                 :                :                                             PG_GETARG_DATUM(RANGE_BOUNDS_HISTOGRAM_ARG),
                                505                 :                :                                             atttypid, atttypmod,
                                506                 :                :                                             &converted);
                                507                 :                : 
   16 michael@paquier.xyz       508   [ +  -  +  + ]:GNC          48 :         if (converted &&
                                509                 :             24 :             statatt_check_bounds_histogram(stavalues))
                                510                 :                :         {
  212 michael@paquier.xyz       511                 :CBC          16 :             statatt_set_slot(values, nulls, replaces,
                                512                 :                :                              STATISTIC_KIND_BOUNDS_HISTOGRAM,
                                513                 :                :                              InvalidOid, InvalidOid,
                                514                 :                :                              0, true, stavalues, false);
                                515                 :                :         }
                                516                 :                :         else
  641 jdavis@postgresql.or      517                 :GBC           8 :             result = false;
                                518                 :                :     }
                                519                 :                : 
                                520                 :                :     /* STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM */
  641 jdavis@postgresql.or      521         [ +  + ]:CBC         841 :     if (do_range_length_histogram)
                                522                 :                :     {
                                523                 :                :         /* The anyarray is always a float8[] for this stakind */
                                524                 :             16 :         Datum       elems[] = {PG_GETARG_DATUM(RANGE_EMPTY_FRAC_ARG)};
                                525                 :             16 :         ArrayType  *arry = construct_array_builtin(elems, 1, FLOAT4OID);
                                526                 :             16 :         Datum       stanumbers = PointerGetDatum(arry);
                                527                 :                : 
                                528                 :             16 :         bool        converted = false;
                                529                 :                :         Datum       stavalues;
                                530                 :                : 
  212 michael@paquier.xyz       531                 :             16 :         stavalues = statatt_build_stavalues("range_length_histogram",
                                532                 :                :                                             &array_in_fn,
                                533                 :                :                                             PG_GETARG_DATUM(RANGE_LENGTH_HISTOGRAM_ARG),
                                534                 :                :                                             FLOAT8OID, 0, &converted);
                                535                 :                : 
  641 jdavis@postgresql.or      536         [ +  - ]:             16 :         if (converted)
                                537                 :                :         {
  212 michael@paquier.xyz       538                 :             16 :             statatt_set_slot(values, nulls, replaces,
                                539                 :                :                              STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM,
                                540                 :                :                              Float8LessOperator, InvalidOid,
                                541                 :                :                              stanumbers, false, stavalues, false);
                                542                 :                :         }
                                543                 :                :         else
  641 jdavis@postgresql.or      544                 :UBC           0 :             result = false;
                                545                 :                :     }
                                546                 :                : 
  641 jdavis@postgresql.or      547                 :CBC         841 :     upsert_pg_statistic(starel, statup, values, nulls, replaces);
                                548                 :                : 
                                549         [ +  + ]:            841 :     if (HeapTupleIsValid(statup))
                                550                 :            104 :         ReleaseSysCache(statup);
                                551                 :            841 :     table_close(starel, RowExclusiveLock);
                                552                 :                : 
                                553                 :            841 :     return result;
                                554                 :                : }
                                555                 :                : 
                                556                 :                : /*
                                557                 :                :  * Upsert the pg_statistic record.
                                558                 :                :  */
                                559                 :                : static void
                                560                 :            841 : upsert_pg_statistic(Relation starel, HeapTuple oldtup,
                                561                 :                :                     const Datum *values, const bool *nulls, const bool *replaces)
                                562                 :                : {
                                563                 :                :     HeapTuple   newtup;
                                564                 :                : 
                                565         [ +  + ]:            841 :     if (HeapTupleIsValid(oldtup))
                                566                 :                :     {
                                567                 :            104 :         newtup = heap_modify_tuple(oldtup, RelationGetDescr(starel),
                                568                 :                :                                    values, nulls, replaces);
                                569                 :            104 :         CatalogTupleUpdate(starel, &newtup->t_self, newtup);
                                570                 :                :     }
                                571                 :                :     else
                                572                 :                :     {
                                573                 :            737 :         newtup = heap_form_tuple(RelationGetDescr(starel), values, nulls);
                                574                 :            737 :         CatalogTupleInsert(starel, newtup);
                                575                 :                :     }
                                576                 :                : 
                                577                 :            841 :     heap_freetuple(newtup);
                                578                 :                : 
  634                           579                 :            841 :     CommandCounterIncrement();
  641                           580                 :            841 : }
                                581                 :                : 
                                582                 :                : /*
                                583                 :                :  * Delete pg_statistic record.
                                584                 :                :  */
                                585                 :                : static bool
                                586                 :             15 : delete_pg_statistic(Oid reloid, AttrNumber attnum, bool stainherit)
                                587                 :                : {
                                588                 :             15 :     Relation    sd = table_open(StatisticRelationId, RowExclusiveLock);
                                589                 :                :     HeapTuple   oldtup;
  634                           590                 :             15 :     bool        result = false;
                                591                 :                : 
                                592                 :                :     /* Is there already a pg_statistic tuple for this attribute? */
  641                           593                 :             15 :     oldtup = SearchSysCache3(STATRELATTINH,
                                594                 :                :                              ObjectIdGetDatum(reloid),
                                595                 :                :                              Int16GetDatum(attnum),
                                596                 :                :                              BoolGetDatum(stainherit));
                                597                 :                : 
                                598         [ +  + ]:             15 :     if (HeapTupleIsValid(oldtup))
                                599                 :                :     {
                                600                 :             13 :         CatalogTupleDelete(sd, &oldtup->t_self);
                                601                 :             13 :         ReleaseSysCache(oldtup);
  634                           602                 :             13 :         result = true;
                                603                 :                :     }
                                604                 :                : 
  641                           605                 :             15 :     table_close(sd, RowExclusiveLock);
                                606                 :                : 
  634                           607                 :             15 :     CommandCounterIncrement();
                                608                 :                : 
                                609                 :             15 :     return result;
                                610                 :                : }
                                611                 :                : 
                                612                 :                : /*
                                613                 :                :  * Delete statistics for the given attribute.
                                614                 :                :  */
                                615                 :                : Datum
  641                           616                 :              4 : pg_clear_attribute_stats(PG_FUNCTION_ARGS)
                                617                 :                : {
                                618                 :                :     char       *nspname;
                                619                 :                :     char       *relname;
                                620                 :                :     Oid         reloid;
                                621                 :                :     char       *attname;
                                622                 :                :     AttrNumber  attnum;
                                623                 :                :     bool        inherited;
  283 nathan@postgresql.or      624                 :              4 :     Oid         locked_table = InvalidOid;
                                625                 :                : 
  487 jdavis@postgresql.or      626                 :              4 :     stats_check_required_arg(fcinfo, cleararginfo, C_ATTRELSCHEMA_ARG);
                                627                 :              4 :     stats_check_required_arg(fcinfo, cleararginfo, C_ATTRELNAME_ARG);
                                628                 :              4 :     stats_check_required_arg(fcinfo, cleararginfo, C_ATTNAME_ARG);
                                629                 :              4 :     stats_check_required_arg(fcinfo, cleararginfo, C_INHERITED_ARG);
                                630                 :                : 
                                631                 :              4 :     nspname = TextDatumGetCString(PG_GETARG_DATUM(C_ATTRELSCHEMA_ARG));
                                632                 :              4 :     relname = TextDatumGetCString(PG_GETARG_DATUM(C_ATTRELNAME_ARG));
                                633                 :                : 
  612 fujii@postgresql.org      634         [ -  + ]:              4 :     if (RecoveryInProgress())
  612 fujii@postgresql.org      635         [ #  # ]:UBC           0 :         ereport(ERROR,
                                636                 :                :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                                637                 :                :                  errmsg("recovery is in progress"),
                                638                 :                :                  errhint("Statistics cannot be modified during recovery.")));
                                639                 :                : 
  283 nathan@postgresql.or      640                 :CBC           4 :     reloid = RangeVarGetRelidExtended(makeRangeVar(nspname, relname, -1),
                                641                 :                :                                       ShareUpdateExclusiveLock, 0,
                                642                 :                :                                       RangeVarCallbackForStats, &locked_table);
                                643                 :                : 
  487 jdavis@postgresql.or      644                 :              4 :     attname = TextDatumGetCString(PG_GETARG_DATUM(C_ATTNAME_ARG));
                                645                 :              4 :     attnum = get_attnum(reloid, attname);
                                646                 :                : 
  610                           647         [ -  + ]:              4 :     if (attnum < 0)
  610 jdavis@postgresql.or      648         [ #  # ]:UBC           0 :         ereport(ERROR,
                                649                 :                :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                650                 :                :                  errmsg("cannot clear statistics on system column \"%s\"",
                                651                 :                :                         attname)));
                                652                 :                : 
  640 jdavis@postgresql.or      653         [ -  + ]:CBC           4 :     if (attnum == InvalidAttrNumber)
  640 jdavis@postgresql.or      654         [ #  # ]:UBC           0 :         ereport(ERROR,
                                655                 :                :                 (errcode(ERRCODE_UNDEFINED_COLUMN),
                                656                 :                :                  errmsg("column \"%s\" of relation \"%s\" does not exist",
                                657                 :                :                         attname, get_rel_name(reloid))));
                                658                 :                : 
  514 tgl@sss.pgh.pa.us         659                 :CBC           4 :     inherited = PG_GETARG_BOOL(C_INHERITED_ARG);
                                660                 :                : 
  641 jdavis@postgresql.or      661                 :              4 :     delete_pg_statistic(reloid, attnum, inherited);
                                662                 :              4 :     PG_RETURN_VOID();
                                663                 :                : }
                                664                 :                : 
                                665                 :                : /*
                                666                 :                :  * Import statistics for a given relation attribute.
                                667                 :                :  *
                                668                 :                :  * Inserts or replaces a row in pg_statistic for the given relation and
                                669                 :                :  * attribute name or number. It takes input parameters that correspond to
                                670                 :                :  * columns in the view pg_stats.
                                671                 :                :  *
                                672                 :                :  * Parameters are given in a pseudo named-attribute style: they must be
                                673                 :                :  * pairs of parameter names (as text) and values (of appropriate types).
                                674                 :                :  * We do that, rather than using regular named-parameter notation, so
                                675                 :                :  * that we can add or change parameters without fear of breaking
                                676                 :                :  * carelessly-written calls.
                                677                 :                :  *
                                678                 :                :  * Parameters null_frac, avg_width, and n_distinct all correspond to NOT NULL
                                679                 :                :  * columns in pg_statistic. The remaining parameters all belong to a specific
                                680                 :                :  * stakind. Some stakinds require multiple parameters, which must be specified
                                681                 :                :  * together (or neither specified).
                                682                 :                :  *
                                683                 :                :  * Parameters are only superficially validated. Omitting a parameter or
                                684                 :                :  * passing NULL leaves the statistic unchanged.
                                685                 :                :  *
                                686                 :                :  * Parameters corresponding to ANYARRAY columns are instead passed in as text
                                687                 :                :  * values, which is a valid input string for an array of the type or element
                                688                 :                :  * type of the attribute. Any error generated by the array_in() function will
                                689                 :                :  * in turn fail the function.
                                690                 :                :  */
                                691                 :                : Datum
  639                           692                 :            874 : pg_restore_attribute_stats(PG_FUNCTION_ARGS)
                                693                 :                : {
                                694                 :            874 :     LOCAL_FCINFO(positional_fcinfo, NUM_ATTRIBUTE_STATS_ARGS);
                                695                 :            874 :     bool        result = true;
                                696                 :                : 
                                697                 :            874 :     InitFunctionCallInfoData(*positional_fcinfo, NULL, NUM_ATTRIBUTE_STATS_ARGS,
                                698                 :                :                              InvalidOid, NULL, NULL);
                                699                 :                : 
                                700         [ +  + ]:            874 :     if (!stats_fill_fcinfo_from_arg_pairs(fcinfo, positional_fcinfo,
                                701                 :                :                                           attarginfo))
                                702                 :              8 :         result = false;
                                703                 :                : 
  515                           704         [ +  + ]:            874 :     if (!attribute_statistics_update(positional_fcinfo))
  639                           705                 :             80 :         result = false;
                                706                 :                : 
                                707                 :            830 :     PG_RETURN_BOOL(result);
                                708                 :                : }
                                709                 :                : 
                                710                 :                : /*
                                711                 :                :  * Import attribute statistics from NullableDatum inputs for all statistical
                                712                 :                :  * values.
                                713                 :                :  *
                                714                 :                :  * For now, the 'version' argument is ignored. In the future it can be used
                                715                 :                :  * to interpret older statistics properly.
                                716                 :                :  */
                                717                 :                : bool
   15 efujita@postgresql.o      718                 :             11 : import_attribute_statistics(Relation rel, AttrNumber attnum, bool inherited,
                                719                 :                :                             const NullableDatum *version,
                                720                 :                :                             const NullableDatum *null_frac,
                                721                 :                :                             const NullableDatum *avg_width,
                                722                 :                :                             const NullableDatum *n_distinct,
                                723                 :                :                             const NullableDatum *most_common_vals,
                                724                 :                :                             const NullableDatum *most_common_freqs,
                                725                 :                :                             const NullableDatum *histogram_bounds,
                                726                 :                :                             const NullableDatum *correlation,
                                727                 :                :                             const NullableDatum *most_common_elems,
                                728                 :                :                             const NullableDatum *most_common_elem_freqs,
                                729                 :                :                             const NullableDatum *elem_count_histogram,
                                730                 :                :                             const NullableDatum *range_length_histogram,
                                731                 :                :                             const NullableDatum *range_empty_frac,
                                732                 :                :                             const NullableDatum *range_bounds_histogram)
                                733                 :                : {
                                734                 :             11 :     LOCAL_FCINFO(newfcinfo, NUM_ATTRIBUTE_STATS_ARGS);
                                735                 :             11 :     Oid         reloid = RelationGetRelid(rel);
                                736                 :             11 :     char       *relname = RelationGetRelationName(rel);
                                737                 :             11 :     char       *attname = get_attname(reloid, attnum, true);
                                738                 :                : 
                                739         [ -  + ]:             11 :     Assert(null_frac);
                                740         [ -  + ]:             11 :     Assert(avg_width);
                                741         [ -  + ]:             11 :     Assert(n_distinct);
                                742         [ -  + ]:             11 :     Assert(most_common_vals);
                                743         [ -  + ]:             11 :     Assert(most_common_freqs);
                                744         [ -  + ]:             11 :     Assert(histogram_bounds);
                                745         [ -  + ]:             11 :     Assert(correlation);
                                746         [ -  + ]:             11 :     Assert(most_common_elems);
                                747         [ -  + ]:             11 :     Assert(most_common_elem_freqs);
                                748         [ -  + ]:             11 :     Assert(elem_count_histogram);
                                749         [ -  + ]:             11 :     Assert(range_length_histogram);
                                750         [ -  + ]:             11 :     Assert(range_empty_frac);
                                751         [ -  + ]:             11 :     Assert(range_bounds_histogram);
                                752                 :                : 
                                753                 :                :     /* annoyingly, get_attname doesn't check attisdropped */
                                754         [ +  - ]:             11 :     if (attname == NULL ||
                                755         [ -  + ]:             11 :         !SearchSysCacheExistsAttName(reloid, attname))
   15 efujita@postgresql.o      756         [ #  # ]:UBC           0 :         ereport(ERROR,
                                757                 :                :                 (errcode(ERRCODE_UNDEFINED_COLUMN),
                                758                 :                :                  errmsg("column %d of relation \"%s\" does not exist",
                                759                 :                :                         attnum, relname)));
                                760                 :                : 
   15 efujita@postgresql.o      761                 :CBC          11 :     InitFunctionCallInfoData(*newfcinfo, NULL, NUM_ATTRIBUTE_STATS_ARGS,
                                762                 :                :                              InvalidOid, NULL, NULL);
                                763                 :                : 
                                764                 :             11 :     newfcinfo->args[ATTRELSCHEMA_ARG].value =
                                765                 :             11 :         CStringGetTextDatum(get_namespace_name(RelationGetNamespace(rel)));
                                766                 :             11 :     newfcinfo->args[ATTRELSCHEMA_ARG].isnull = false;
                                767                 :             11 :     newfcinfo->args[ATTRELNAME_ARG].value = CStringGetTextDatum(relname);
                                768                 :             11 :     newfcinfo->args[ATTRELNAME_ARG].isnull = false;
                                769                 :             11 :     newfcinfo->args[ATTNAME_ARG].value = CStringGetTextDatum(attname);
                                770                 :             11 :     newfcinfo->args[ATTNAME_ARG].isnull = false;
                                771                 :             11 :     newfcinfo->args[ATTNUM_ARG].value = Int16GetDatum(attnum);
                                772                 :             11 :     newfcinfo->args[ATTNUM_ARG].isnull = false;
                                773                 :             11 :     newfcinfo->args[INHERITED_ARG].value = BoolGetDatum(inherited);
                                774                 :             11 :     newfcinfo->args[INHERITED_ARG].isnull = false;
                                775                 :                : 
                                776                 :             11 :     newfcinfo->args[NULL_FRAC_ARG] = *null_frac;
                                777                 :             11 :     newfcinfo->args[AVG_WIDTH_ARG] = *avg_width;
                                778                 :             11 :     newfcinfo->args[N_DISTINCT_ARG] = *n_distinct;
                                779                 :             11 :     newfcinfo->args[MOST_COMMON_VALS_ARG] = *most_common_vals;
                                780                 :             11 :     newfcinfo->args[MOST_COMMON_FREQS_ARG] = *most_common_freqs;
                                781                 :             11 :     newfcinfo->args[HISTOGRAM_BOUNDS_ARG] = *histogram_bounds;
                                782                 :             11 :     newfcinfo->args[CORRELATION_ARG] = *correlation;
                                783                 :             11 :     newfcinfo->args[MOST_COMMON_ELEMS_ARG] = *most_common_elems;
                                784                 :             11 :     newfcinfo->args[MOST_COMMON_ELEM_FREQS_ARG] = *most_common_elem_freqs;
                                785                 :             11 :     newfcinfo->args[ELEM_COUNT_HISTOGRAM_ARG] = *elem_count_histogram;
                                786                 :             11 :     newfcinfo->args[RANGE_LENGTH_HISTOGRAM_ARG] = *range_length_histogram;
                                787                 :             11 :     newfcinfo->args[RANGE_EMPTY_FRAC_ARG] = *range_empty_frac;
                                788                 :             11 :     newfcinfo->args[RANGE_BOUNDS_HISTOGRAM_ARG] = *range_bounds_histogram;
                                789                 :                : 
                                790                 :             11 :     return attribute_statistics_update_internal(reloid, attname, attnum,
                                791                 :                :                                                 inherited, newfcinfo);
                                792                 :                : }
                                793                 :                : 
                                794                 :                : /*
                                795                 :                :  * Delete attribute statistics.
                                796                 :                :  */
                                797                 :                : bool
                                798                 :             11 : delete_attribute_statistics(Relation rel, AttrNumber attnum, bool inherited)
                                799                 :                : {
                                800                 :             11 :     return delete_pg_statistic(RelationGetRelid(rel), attnum, inherited);
                                801                 :                : }
        

Generated by: LCOV version 2.0-1