LCOV - code coverage report
Current view: top level - src/backend/statistics - attribute_stats.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 298 323 92.3 %
Date: 2025-02-22 07:14:56 Functions: 12 12 100.0 %
Legend: Lines: hit not hit

          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-2025, 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/pg_collation.h"
      23             : #include "catalog/pg_operator.h"
      24             : #include "nodes/nodeFuncs.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             : #define DEFAULT_NULL_FRAC      Float4GetDatum(0.0)
      34             : #define DEFAULT_AVG_WIDTH      Int32GetDatum(0) /* unknown */
      35             : #define DEFAULT_N_DISTINCT     Float4GetDatum(0.0)  /* unknown */
      36             : 
      37             : enum attribute_stats_argnum
      38             : {
      39             :     ATTRELATION_ARG = 0,
      40             :     ATTNAME_ARG,
      41             :     INHERITED_ARG,
      42             :     NULL_FRAC_ARG,
      43             :     AVG_WIDTH_ARG,
      44             :     N_DISTINCT_ARG,
      45             :     MOST_COMMON_VALS_ARG,
      46             :     MOST_COMMON_FREQS_ARG,
      47             :     HISTOGRAM_BOUNDS_ARG,
      48             :     CORRELATION_ARG,
      49             :     MOST_COMMON_ELEMS_ARG,
      50             :     MOST_COMMON_ELEM_FREQS_ARG,
      51             :     ELEM_COUNT_HISTOGRAM_ARG,
      52             :     RANGE_LENGTH_HISTOGRAM_ARG,
      53             :     RANGE_EMPTY_FRAC_ARG,
      54             :     RANGE_BOUNDS_HISTOGRAM_ARG,
      55             :     NUM_ATTRIBUTE_STATS_ARGS
      56             : };
      57             : 
      58             : static struct StatsArgInfo attarginfo[] =
      59             : {
      60             :     [ATTRELATION_ARG] = {"relation", REGCLASSOID},
      61             :     [ATTNAME_ARG] = {"attname", NAMEOID},
      62             :     [INHERITED_ARG] = {"inherited", BOOLOID},
      63             :     [NULL_FRAC_ARG] = {"null_frac", FLOAT4OID},
      64             :     [AVG_WIDTH_ARG] = {"avg_width", INT4OID},
      65             :     [N_DISTINCT_ARG] = {"n_distinct", FLOAT4OID},
      66             :     [MOST_COMMON_VALS_ARG] = {"most_common_vals", TEXTOID},
      67             :     [MOST_COMMON_FREQS_ARG] = {"most_common_freqs", FLOAT4ARRAYOID},
      68             :     [HISTOGRAM_BOUNDS_ARG] = {"histogram_bounds", TEXTOID},
      69             :     [CORRELATION_ARG] = {"correlation", FLOAT4OID},
      70             :     [MOST_COMMON_ELEMS_ARG] = {"most_common_elems", TEXTOID},
      71             :     [MOST_COMMON_ELEM_FREQS_ARG] = {"most_common_elem_freqs", FLOAT4ARRAYOID},
      72             :     [ELEM_COUNT_HISTOGRAM_ARG] = {"elem_count_histogram", FLOAT4ARRAYOID},
      73             :     [RANGE_LENGTH_HISTOGRAM_ARG] = {"range_length_histogram", TEXTOID},
      74             :     [RANGE_EMPTY_FRAC_ARG] = {"range_empty_frac", FLOAT4OID},
      75             :     [RANGE_BOUNDS_HISTOGRAM_ARG] = {"range_bounds_histogram", TEXTOID},
      76             :     [NUM_ATTRIBUTE_STATS_ARGS] = {0}
      77             : };
      78             : 
      79             : static bool attribute_statistics_update(FunctionCallInfo fcinfo, int elevel);
      80             : static Node *get_attr_expr(Relation rel, int attnum);
      81             : static void get_attr_stat_type(Oid reloid, AttrNumber attnum, int elevel,
      82             :                                Oid *atttypid, int32 *atttypmod,
      83             :                                char *atttyptype, Oid *atttypcoll,
      84             :                                Oid *eq_opr, Oid *lt_opr);
      85             : static bool get_elem_stat_type(Oid atttypid, char atttyptype, int elevel,
      86             :                                Oid *elemtypid, Oid *elem_eq_opr);
      87             : static Datum text_to_stavalues(const char *staname, FmgrInfo *array_in, Datum d,
      88             :                                Oid typid, int32 typmod, int elevel, bool *ok);
      89             : static void set_stats_slot(Datum *values, bool *nulls, bool *replaces,
      90             :                            int16 stakind, Oid staop, Oid stacoll,
      91             :                            Datum stanumbers, bool stanumbers_isnull,
      92             :                            Datum stavalues, bool stavalues_isnull);
      93             : static void upsert_pg_statistic(Relation starel, HeapTuple oldtup,
      94             :                                 Datum *values, bool *nulls, bool *replaces);
      95             : static bool delete_pg_statistic(Oid reloid, AttrNumber attnum, bool stainherit);
      96             : static void init_empty_stats_tuple(Oid reloid, int16 attnum, bool inherited,
      97             :                                    Datum *values, bool *nulls, bool *replaces);
      98             : 
      99             : /*
     100             :  * Insert or Update Attribute Statistics
     101             :  *
     102             :  * See pg_statistic.h for an explanation of how each statistic kind is
     103             :  * stored. Custom statistics kinds are not supported.
     104             :  *
     105             :  * Depending on the statistics kind, we need to derive information from the
     106             :  * attribute for which we're storing the stats. For instance, the MCVs are
     107             :  * stored as an anyarray, and the representation of the array needs to store
     108             :  * the correct element type, which must be derived from the attribute.
     109             :  *
     110             :  * Major errors, such as the table not existing, the attribute not existing,
     111             :  * or a permissions failure are always reported at ERROR. Other errors, such
     112             :  * as a conversion failure on one statistic kind, are reported at 'elevel',
     113             :  * and other statistic kinds may still be updated.
     114             :  */
     115             : static bool
     116        1568 : attribute_statistics_update(FunctionCallInfo fcinfo, int elevel)
     117             : {
     118             :     Oid         reloid;
     119             :     Name        attname;
     120             :     bool        inherited;
     121             :     AttrNumber  attnum;
     122             : 
     123             :     Relation    starel;
     124             :     HeapTuple   statup;
     125             : 
     126        1568 :     Oid         atttypid = InvalidOid;
     127             :     int32       atttypmod;
     128             :     char        atttyptype;
     129        1568 :     Oid         atttypcoll = InvalidOid;
     130        1568 :     Oid         eq_opr = InvalidOid;
     131        1568 :     Oid         lt_opr = InvalidOid;
     132             : 
     133        1568 :     Oid         elemtypid = InvalidOid;
     134        1568 :     Oid         elem_eq_opr = InvalidOid;
     135             : 
     136             :     FmgrInfo    array_in_fn;
     137             : 
     138        2254 :     bool        do_mcv = !PG_ARGISNULL(MOST_COMMON_FREQS_ARG) &&
     139         686 :         !PG_ARGISNULL(MOST_COMMON_VALS_ARG);
     140        1568 :     bool        do_histogram = !PG_ARGISNULL(HISTOGRAM_BOUNDS_ARG);
     141        1568 :     bool        do_correlation = !PG_ARGISNULL(CORRELATION_ARG);
     142        1626 :     bool        do_mcelem = !PG_ARGISNULL(MOST_COMMON_ELEMS_ARG) &&
     143          58 :         !PG_ARGISNULL(MOST_COMMON_ELEM_FREQS_ARG);
     144        1568 :     bool        do_dechist = !PG_ARGISNULL(ELEM_COUNT_HISTOGRAM_ARG);
     145        1568 :     bool        do_bounds_histogram = !PG_ARGISNULL(RANGE_BOUNDS_HISTOGRAM_ARG);
     146        1634 :     bool        do_range_length_histogram = !PG_ARGISNULL(RANGE_LENGTH_HISTOGRAM_ARG) &&
     147          66 :         !PG_ARGISNULL(RANGE_EMPTY_FRAC_ARG);
     148             : 
     149        1568 :     Datum       values[Natts_pg_statistic] = {0};
     150        1568 :     bool        nulls[Natts_pg_statistic] = {0};
     151        1568 :     bool        replaces[Natts_pg_statistic] = {0};
     152             : 
     153        1568 :     bool        result = true;
     154             : 
     155        1568 :     stats_check_required_arg(fcinfo, attarginfo, ATTRELATION_ARG);
     156        1556 :     reloid = PG_GETARG_OID(ATTRELATION_ARG);
     157             : 
     158        1556 :     if (RecoveryInProgress())
     159           0 :         ereport(ERROR,
     160             :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
     161             :                  errmsg("recovery is in progress"),
     162             :                  errhint("Statistics cannot be modified during recovery.")));
     163             : 
     164             :     /* lock before looking up attribute */
     165        1556 :     stats_lock_check_privileges(reloid);
     166             : 
     167        1544 :     stats_check_required_arg(fcinfo, attarginfo, ATTNAME_ARG);
     168        1532 :     attname = PG_GETARG_NAME(ATTNAME_ARG);
     169        1532 :     attnum = get_attnum(reloid, NameStr(*attname));
     170             : 
     171        1532 :     if (attnum < 0)
     172           6 :         ereport(ERROR,
     173             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     174             :                  errmsg("cannot modify statistics on system column \"%s\"",
     175             :                         NameStr(*attname))));
     176             : 
     177        1526 :     if (attnum == InvalidAttrNumber)
     178          12 :         ereport(ERROR,
     179             :                 (errcode(ERRCODE_UNDEFINED_COLUMN),
     180             :                  errmsg("column \"%s\" of relation \"%s\" does not exist",
     181             :                         NameStr(*attname), get_rel_name(reloid))));
     182             : 
     183        1514 :     stats_check_required_arg(fcinfo, attarginfo, INHERITED_ARG);
     184        1502 :     inherited = PG_GETARG_BOOL(INHERITED_ARG);
     185             : 
     186             :     /*
     187             :      * Check argument sanity. If some arguments are unusable, emit at elevel
     188             :      * and set the corresponding argument to NULL in fcinfo.
     189             :      */
     190             : 
     191        1502 :     if (!stats_check_arg_array(fcinfo, attarginfo, MOST_COMMON_FREQS_ARG,
     192             :                                elevel))
     193             :     {
     194           0 :         do_mcv = false;
     195           0 :         result = false;
     196             :     }
     197             : 
     198        1502 :     if (!stats_check_arg_array(fcinfo, attarginfo, MOST_COMMON_ELEM_FREQS_ARG,
     199             :                                elevel))
     200             :     {
     201           0 :         do_mcelem = false;
     202           0 :         result = false;
     203             :     }
     204        1502 :     if (!stats_check_arg_array(fcinfo, attarginfo, ELEM_COUNT_HISTOGRAM_ARG,
     205             :                                elevel))
     206             :     {
     207           6 :         do_dechist = false;
     208           6 :         result = false;
     209             :     }
     210             : 
     211        1496 :     if (!stats_check_arg_pair(fcinfo, attarginfo,
     212             :                               MOST_COMMON_VALS_ARG, MOST_COMMON_FREQS_ARG,
     213             :                               elevel))
     214             :     {
     215          18 :         do_mcv = false;
     216          18 :         result = false;
     217             :     }
     218             : 
     219        1484 :     if (!stats_check_arg_pair(fcinfo, attarginfo,
     220             :                               MOST_COMMON_ELEMS_ARG,
     221             :                               MOST_COMMON_ELEM_FREQS_ARG, elevel))
     222             :     {
     223           0 :         do_mcelem = false;
     224           0 :         result = false;
     225             :     }
     226             : 
     227        1472 :     if (!stats_check_arg_pair(fcinfo, attarginfo,
     228             :                               RANGE_LENGTH_HISTOGRAM_ARG,
     229             :                               RANGE_EMPTY_FRAC_ARG, elevel))
     230             :     {
     231          12 :         do_range_length_histogram = false;
     232          12 :         result = false;
     233             :     }
     234             : 
     235             :     /* derive information from attribute */
     236        1460 :     get_attr_stat_type(reloid, attnum, elevel,
     237             :                        &atttypid, &atttypmod,
     238             :                        &atttyptype, &atttypcoll,
     239             :                        &eq_opr, &lt_opr);
     240             : 
     241             :     /* if needed, derive element type */
     242        1460 :     if (do_mcelem || do_dechist)
     243             :     {
     244          70 :         if (!get_elem_stat_type(atttypid, atttyptype, elevel,
     245             :                                 &elemtypid, &elem_eq_opr))
     246             :         {
     247          24 :             ereport(elevel,
     248             :                     (errmsg("unable to determine element type of attribute \"%s\"", NameStr(*attname)),
     249             :                      errdetail("Cannot set STATISTIC_KIND_MCELEM or STATISTIC_KIND_DECHIST.")));
     250           6 :             elemtypid = InvalidOid;
     251           6 :             elem_eq_opr = InvalidOid;
     252             : 
     253           6 :             do_mcelem = false;
     254           6 :             do_dechist = false;
     255           6 :             result = false;
     256             :         }
     257             :     }
     258             : 
     259             :     /* histogram and correlation require less-than operator */
     260        1442 :     if ((do_histogram || do_correlation) && !OidIsValid(lt_opr))
     261             :     {
     262           0 :         ereport(elevel,
     263             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     264             :                  errmsg("could not determine less-than operator for attribute \"%s\"", NameStr(*attname)),
     265             :                  errdetail("Cannot set STATISTIC_KIND_HISTOGRAM or STATISTIC_KIND_CORRELATION.")));
     266             : 
     267           0 :         do_histogram = false;
     268           0 :         do_correlation = false;
     269           0 :         result = false;
     270             :     }
     271             : 
     272             :     /* only range types can have range stats */
     273        1442 :     if ((do_range_length_histogram || do_bounds_histogram) &&
     274          72 :         !(atttyptype == TYPTYPE_RANGE || atttyptype == TYPTYPE_MULTIRANGE))
     275             :     {
     276          24 :         ereport(elevel,
     277             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     278             :                  errmsg("attribute \"%s\" is not a range type", NameStr(*attname)),
     279             :                  errdetail("Cannot set STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM or STATISTIC_KIND_BOUNDS_HISTOGRAM.")));
     280             : 
     281          12 :         do_bounds_histogram = false;
     282          12 :         do_range_length_histogram = false;
     283          12 :         result = false;
     284             :     }
     285             : 
     286        1430 :     fmgr_info(F_ARRAY_IN, &array_in_fn);
     287             : 
     288        1430 :     starel = table_open(StatisticRelationId, RowExclusiveLock);
     289             : 
     290        1430 :     statup = SearchSysCache3(STATRELATTINH, reloid, attnum, inherited);
     291             : 
     292             :     /* initialize from existing tuple if exists */
     293        1430 :     if (HeapTupleIsValid(statup))
     294         138 :         heap_deform_tuple(statup, RelationGetDescr(starel), values, nulls);
     295             :     else
     296        1292 :         init_empty_stats_tuple(reloid, attnum, inherited, values, nulls,
     297             :                                replaces);
     298             : 
     299             :     /* if specified, set to argument values */
     300        1430 :     if (!PG_ARGISNULL(NULL_FRAC_ARG))
     301             :     {
     302        1430 :         values[Anum_pg_statistic_stanullfrac - 1] = PG_GETARG_DATUM(NULL_FRAC_ARG);
     303        1430 :         replaces[Anum_pg_statistic_stanullfrac - 1] = true;
     304             :     }
     305        1430 :     if (!PG_ARGISNULL(AVG_WIDTH_ARG))
     306             :     {
     307        1424 :         values[Anum_pg_statistic_stawidth - 1] = PG_GETARG_DATUM(AVG_WIDTH_ARG);
     308        1424 :         replaces[Anum_pg_statistic_stawidth - 1] = true;
     309             :     }
     310        1430 :     if (!PG_ARGISNULL(N_DISTINCT_ARG))
     311             :     {
     312        1424 :         values[Anum_pg_statistic_stadistinct - 1] = PG_GETARG_DATUM(N_DISTINCT_ARG);
     313        1424 :         replaces[Anum_pg_statistic_stadistinct - 1] = true;
     314             :     }
     315             : 
     316             :     /* STATISTIC_KIND_MCV */
     317        1430 :     if (do_mcv)
     318             :     {
     319             :         bool        converted;
     320         668 :         Datum       stanumbers = PG_GETARG_DATUM(MOST_COMMON_FREQS_ARG);
     321         668 :         Datum       stavalues = text_to_stavalues("most_common_vals",
     322             :                                                   &array_in_fn,
     323             :                                                   PG_GETARG_DATUM(MOST_COMMON_VALS_ARG),
     324             :                                                   atttypid, atttypmod,
     325             :                                                   elevel, &converted);
     326             : 
     327         656 :         if (converted)
     328             :         {
     329         650 :             set_stats_slot(values, nulls, replaces,
     330             :                            STATISTIC_KIND_MCV,
     331             :                            eq_opr, atttypcoll,
     332             :                            stanumbers, false, stavalues, false);
     333             :         }
     334             :         else
     335           6 :             result = false;
     336             :     }
     337             : 
     338             :     /* STATISTIC_KIND_HISTOGRAM */
     339        1418 :     if (do_histogram)
     340             :     {
     341             :         Datum       stavalues;
     342         658 :         bool        converted = false;
     343             : 
     344         658 :         stavalues = text_to_stavalues("histogram_bounds",
     345             :                                       &array_in_fn,
     346             :                                       PG_GETARG_DATUM(HISTOGRAM_BOUNDS_ARG),
     347             :                                       atttypid, atttypmod, elevel,
     348             :                                       &converted);
     349             : 
     350         652 :         if (converted)
     351             :         {
     352         646 :             set_stats_slot(values, nulls, replaces,
     353             :                            STATISTIC_KIND_HISTOGRAM,
     354             :                            lt_opr, atttypcoll,
     355             :                            0, true, stavalues, false);
     356             :         }
     357             :         else
     358           6 :             result = false;
     359             :     }
     360             : 
     361             :     /* STATISTIC_KIND_CORRELATION */
     362        1412 :     if (do_correlation)
     363             :     {
     364        1194 :         Datum       elems[] = {PG_GETARG_DATUM(CORRELATION_ARG)};
     365        1194 :         ArrayType  *arry = construct_array_builtin(elems, 1, FLOAT4OID);
     366        1194 :         Datum       stanumbers = PointerGetDatum(arry);
     367             : 
     368        1194 :         set_stats_slot(values, nulls, replaces,
     369             :                        STATISTIC_KIND_CORRELATION,
     370             :                        lt_opr, atttypcoll,
     371             :                        stanumbers, false, 0, true);
     372             :     }
     373             : 
     374             :     /* STATISTIC_KIND_MCELEM */
     375        1412 :     if (do_mcelem)
     376             :     {
     377          34 :         Datum       stanumbers = PG_GETARG_DATUM(MOST_COMMON_ELEM_FREQS_ARG);
     378          34 :         bool        converted = false;
     379             :         Datum       stavalues;
     380             : 
     381          34 :         stavalues = text_to_stavalues("most_common_elems",
     382             :                                       &array_in_fn,
     383             :                                       PG_GETARG_DATUM(MOST_COMMON_ELEMS_ARG),
     384             :                                       elemtypid, atttypmod,
     385             :                                       elevel, &converted);
     386             : 
     387          34 :         if (converted)
     388             :         {
     389          34 :             set_stats_slot(values, nulls, replaces,
     390             :                            STATISTIC_KIND_MCELEM,
     391             :                            elem_eq_opr, atttypcoll,
     392             :                            stanumbers, false, stavalues, false);
     393             :         }
     394             :         else
     395           0 :             result = false;
     396             :     }
     397             : 
     398             :     /* STATISTIC_KIND_DECHIST */
     399        1412 :     if (do_dechist)
     400             :     {
     401          38 :         Datum       stanumbers = PG_GETARG_DATUM(ELEM_COUNT_HISTOGRAM_ARG);
     402             : 
     403          38 :         set_stats_slot(values, nulls, replaces,
     404             :                        STATISTIC_KIND_DECHIST,
     405             :                        elem_eq_opr, atttypcoll,
     406             :                        stanumbers, false, 0, true);
     407             :     }
     408             : 
     409             :     /*
     410             :      * STATISTIC_KIND_BOUNDS_HISTOGRAM
     411             :      *
     412             :      * This stakind appears before STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM even
     413             :      * though it is numerically greater, and all other stakinds appear in
     414             :      * numerical order. We duplicate this quirk for consistency.
     415             :      */
     416        1412 :     if (do_bounds_histogram)
     417             :     {
     418          36 :         bool        converted = false;
     419             :         Datum       stavalues;
     420             : 
     421          36 :         stavalues = text_to_stavalues("range_bounds_histogram",
     422             :                                       &array_in_fn,
     423             :                                       PG_GETARG_DATUM(RANGE_BOUNDS_HISTOGRAM_ARG),
     424             :                                       atttypid, atttypmod,
     425             :                                       elevel, &converted);
     426             : 
     427          36 :         if (converted)
     428             :         {
     429          36 :             set_stats_slot(values, nulls, replaces,
     430             :                            STATISTIC_KIND_BOUNDS_HISTOGRAM,
     431             :                            InvalidOid, InvalidOid,
     432             :                            0, true, stavalues, false);
     433             :         }
     434             :         else
     435           0 :             result = false;
     436             :     }
     437             : 
     438             :     /* STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM */
     439        1412 :     if (do_range_length_histogram)
     440             :     {
     441             :         /* The anyarray is always a float8[] for this stakind */
     442          36 :         Datum       elems[] = {PG_GETARG_DATUM(RANGE_EMPTY_FRAC_ARG)};
     443          36 :         ArrayType  *arry = construct_array_builtin(elems, 1, FLOAT4OID);
     444          36 :         Datum       stanumbers = PointerGetDatum(arry);
     445             : 
     446          36 :         bool        converted = false;
     447             :         Datum       stavalues;
     448             : 
     449          36 :         stavalues = text_to_stavalues("range_length_histogram",
     450             :                                       &array_in_fn,
     451             :                                       PG_GETARG_DATUM(RANGE_LENGTH_HISTOGRAM_ARG),
     452             :                                       FLOAT8OID, 0, elevel, &converted);
     453             : 
     454          36 :         if (converted)
     455             :         {
     456          36 :             set_stats_slot(values, nulls, replaces,
     457             :                            STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM,
     458             :                            Float8LessOperator, InvalidOid,
     459             :                            stanumbers, false, stavalues, false);
     460             :         }
     461             :         else
     462           0 :             result = false;
     463             :     }
     464             : 
     465        1412 :     upsert_pg_statistic(starel, statup, values, nulls, replaces);
     466             : 
     467        1412 :     if (HeapTupleIsValid(statup))
     468         120 :         ReleaseSysCache(statup);
     469        1412 :     table_close(starel, RowExclusiveLock);
     470             : 
     471        1412 :     return result;
     472             : }
     473             : 
     474             : /*
     475             :  * If this relation is an index and that index has expressions in it, and
     476             :  * the attnum specified is known to be an expression, then we must walk
     477             :  * the list attributes up to the specified attnum to get the right
     478             :  * expression.
     479             :  */
     480             : static Node *
     481        1460 : get_attr_expr(Relation rel, int attnum)
     482             : {
     483             :     List       *index_exprs;
     484             :     ListCell   *indexpr_item;
     485             : 
     486             :     /* relation is not an index */
     487        1460 :     if (rel->rd_rel->relkind != RELKIND_INDEX &&
     488        1440 :         rel->rd_rel->relkind != RELKIND_PARTITIONED_INDEX)
     489        1440 :         return NULL;
     490             : 
     491          20 :     index_exprs = RelationGetIndexExpressions(rel);
     492             : 
     493             :     /* index has no expressions to give */
     494          20 :     if (index_exprs == NIL)
     495           0 :         return NULL;
     496             : 
     497             :     /*
     498             :      * The index attnum points directly to a relation attnum, then it's no an
     499             :      * expression attribute.
     500             :      */
     501          20 :     if (rel->rd_index->indkey.values[attnum - 1] != 0)
     502           0 :         return NULL;
     503             : 
     504          20 :     indexpr_item = list_head(rel->rd_indexprs);
     505             : 
     506          20 :     for (int i = 0; i < attnum - 1; i++)
     507           0 :         if (rel->rd_index->indkey.values[i] == 0)
     508           0 :             indexpr_item = lnext(rel->rd_indexprs, indexpr_item);
     509             : 
     510          20 :     if (indexpr_item == NULL)   /* shouldn't happen */
     511           0 :         elog(ERROR, "too few entries in indexprs list");
     512             : 
     513          20 :     return (Node *) lfirst(indexpr_item);
     514             : }
     515             : 
     516             : /*
     517             :  * Derive type information from the attribute.
     518             :  */
     519             : static void
     520        1460 : get_attr_stat_type(Oid reloid, AttrNumber attnum, int elevel,
     521             :                    Oid *atttypid, int32 *atttypmod,
     522             :                    char *atttyptype, Oid *atttypcoll,
     523             :                    Oid *eq_opr, Oid *lt_opr)
     524             : {
     525        1460 :     Relation    rel = relation_open(reloid, AccessShareLock);
     526             :     Form_pg_attribute attr;
     527             :     HeapTuple   atup;
     528             :     Node       *expr;
     529             :     TypeCacheEntry *typcache;
     530             : 
     531        1460 :     atup = SearchSysCache2(ATTNUM, ObjectIdGetDatum(reloid),
     532             :                            Int16GetDatum(attnum));
     533             : 
     534             :     /* Attribute not found */
     535        1460 :     if (!HeapTupleIsValid(atup))
     536           0 :         ereport(ERROR,
     537             :                 (errcode(ERRCODE_UNDEFINED_COLUMN),
     538             :                  errmsg("attribute %d of relation \"%s\" does not exist",
     539             :                         attnum, RelationGetRelationName(rel))));
     540             : 
     541        1460 :     attr = (Form_pg_attribute) GETSTRUCT(atup);
     542             : 
     543        1460 :     if (attr->attisdropped)
     544           0 :         ereport(ERROR,
     545             :                 (errcode(ERRCODE_UNDEFINED_COLUMN),
     546             :                  errmsg("attribute %d of relation \"%s\" does not exist",
     547             :                         attnum, RelationGetRelationName(rel))));
     548             : 
     549        1460 :     expr = get_attr_expr(rel, attr->attnum);
     550             : 
     551             :     /*
     552             :      * When analyzing an expression index, believe the expression tree's type
     553             :      * not the column datatype --- the latter might be the opckeytype storage
     554             :      * type of the opclass, which is not interesting for our purposes. This
     555             :      * mimics the behavior of examine_attribute().
     556             :      */
     557        1460 :     if (expr == NULL)
     558             :     {
     559        1440 :         *atttypid = attr->atttypid;
     560        1440 :         *atttypmod = attr->atttypmod;
     561        1440 :         *atttypcoll = attr->attcollation;
     562             :     }
     563             :     else
     564             :     {
     565          20 :         *atttypid = exprType(expr);
     566          20 :         *atttypmod = exprTypmod(expr);
     567             : 
     568          20 :         if (OidIsValid(attr->attcollation))
     569           0 :             *atttypcoll = attr->attcollation;
     570             :         else
     571          20 :             *atttypcoll = exprCollation(expr);
     572             :     }
     573        1460 :     ReleaseSysCache(atup);
     574             : 
     575             :     /*
     576             :      * If it's a multirange, step down to the range type, as is done by
     577             :      * multirange_typanalyze().
     578             :      */
     579        1460 :     if (type_is_multirange(*atttypid))
     580           2 :         *atttypid = get_multirange_range(*atttypid);
     581             : 
     582             :     /* finds the right operators even if atttypid is a domain */
     583        1460 :     typcache = lookup_type_cache(*atttypid, TYPECACHE_LT_OPR | TYPECACHE_EQ_OPR);
     584        1460 :     *atttyptype = typcache->typtype;
     585        1460 :     *eq_opr = typcache->eq_opr;
     586        1460 :     *lt_opr = typcache->lt_opr;
     587             : 
     588             :     /*
     589             :      * Special case: collation for tsvector is DEFAULT_COLLATION_OID. See
     590             :      * compute_tsvector_stats().
     591             :      */
     592        1460 :     if (*atttypid == TSVECTOROID)
     593           2 :         *atttypcoll = DEFAULT_COLLATION_OID;
     594             : 
     595        1460 :     relation_close(rel, NoLock);
     596        1460 : }
     597             : 
     598             : /*
     599             :  * Derive element type information from the attribute type.
     600             :  */
     601             : static bool
     602          70 : get_elem_stat_type(Oid atttypid, char atttyptype, int elevel,
     603             :                    Oid *elemtypid, Oid *elem_eq_opr)
     604             : {
     605             :     TypeCacheEntry *elemtypcache;
     606             : 
     607          70 :     if (atttypid == TSVECTOROID)
     608             :     {
     609             :         /*
     610             :          * Special case: element type for tsvector is text. See
     611             :          * compute_tsvector_stats().
     612             :          */
     613           2 :         *elemtypid = TEXTOID;
     614             :     }
     615             :     else
     616             :     {
     617             :         /* find underlying element type through any domain */
     618          68 :         *elemtypid = get_base_element_type(atttypid);
     619             :     }
     620             : 
     621          70 :     if (!OidIsValid(*elemtypid))
     622          24 :         return false;
     623             : 
     624             :     /* finds the right operator even if elemtypid is a domain */
     625          46 :     elemtypcache = lookup_type_cache(*elemtypid, TYPECACHE_EQ_OPR);
     626          46 :     if (!OidIsValid(elemtypcache->eq_opr))
     627           0 :         return false;
     628             : 
     629          46 :     *elem_eq_opr = elemtypcache->eq_opr;
     630             : 
     631          46 :     return true;
     632             : }
     633             : 
     634             : /*
     635             :  * Cast a text datum into an array with element type elemtypid.
     636             :  *
     637             :  * If an error is encountered, capture it and re-throw at elevel, and set ok
     638             :  * to false. If the resulting array contains NULLs, raise an error at elevel
     639             :  * and set ok to false. Otherwise, set ok to true.
     640             :  */
     641             : static Datum
     642        1432 : text_to_stavalues(const char *staname, FmgrInfo *array_in, Datum d, Oid typid,
     643             :                   int32 typmod, int elevel, bool *ok)
     644             : {
     645        1432 :     LOCAL_FCINFO(fcinfo, 8);
     646             :     char       *s;
     647             :     Datum       result;
     648        1432 :     ErrorSaveContext escontext = {T_ErrorSaveContext};
     649             : 
     650        1432 :     escontext.details_wanted = true;
     651             : 
     652        1432 :     s = TextDatumGetCString(d);
     653             : 
     654        1432 :     InitFunctionCallInfoData(*fcinfo, array_in, 3, InvalidOid,
     655             :                              (Node *) &escontext, NULL);
     656             : 
     657        1432 :     fcinfo->args[0].value = CStringGetDatum(s);
     658        1432 :     fcinfo->args[0].isnull = false;
     659        1432 :     fcinfo->args[1].value = ObjectIdGetDatum(typid);
     660        1432 :     fcinfo->args[1].isnull = false;
     661        1432 :     fcinfo->args[2].value = Int32GetDatum(typmod);
     662        1432 :     fcinfo->args[2].isnull = false;
     663             : 
     664        1432 :     result = FunctionCallInvoke(fcinfo);
     665             : 
     666        1432 :     pfree(s);
     667             : 
     668        1432 :     if (escontext.error_occurred)
     669             :     {
     670          18 :         if (elevel != ERROR)
     671           6 :             escontext.error_data->elevel = elevel;
     672          18 :         ThrowErrorData(escontext.error_data);
     673           6 :         *ok = false;
     674           6 :         return (Datum) 0;
     675             :     }
     676             : 
     677        1414 :     if (array_contains_nulls(DatumGetArrayTypeP(result)))
     678             :     {
     679          12 :         ereport(elevel,
     680             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     681             :                  errmsg("\"%s\" array cannot contain NULL values", staname)));
     682           6 :         *ok = false;
     683           6 :         return (Datum) 0;
     684             :     }
     685             : 
     686        1402 :     *ok = true;
     687             : 
     688        1402 :     return result;
     689             : }
     690             : 
     691             : /*
     692             :  * Find and update the slot with the given stakind, or use the first empty
     693             :  * slot.
     694             :  */
     695             : static void
     696        2634 : set_stats_slot(Datum *values, bool *nulls, bool *replaces,
     697             :                int16 stakind, Oid staop, Oid stacoll,
     698             :                Datum stanumbers, bool stanumbers_isnull,
     699             :                Datum stavalues, bool stavalues_isnull)
     700             : {
     701             :     int         slotidx;
     702        2634 :     int         first_empty = -1;
     703             :     AttrNumber  stakind_attnum;
     704             :     AttrNumber  staop_attnum;
     705             :     AttrNumber  stacoll_attnum;
     706             : 
     707             :     /* find existing slot with given stakind */
     708       15750 :     for (slotidx = 0; slotidx < STATISTIC_NUM_SLOTS; slotidx++)
     709             :     {
     710       13128 :         stakind_attnum = Anum_pg_statistic_stakind1 - 1 + slotidx;
     711             : 
     712       17350 :         if (first_empty < 0 &&
     713        4222 :             DatumGetInt16(values[stakind_attnum]) == 0)
     714        2622 :             first_empty = slotidx;
     715       13128 :         if (DatumGetInt16(values[stakind_attnum]) == stakind)
     716          12 :             break;
     717             :     }
     718             : 
     719        2634 :     if (slotidx >= STATISTIC_NUM_SLOTS && first_empty >= 0)
     720        2622 :         slotidx = first_empty;
     721             : 
     722        2634 :     if (slotidx >= STATISTIC_NUM_SLOTS)
     723           0 :         ereport(ERROR,
     724             :                 (errmsg("maximum number of statistics slots exceeded: %d",
     725             :                         slotidx + 1)));
     726             : 
     727        2634 :     stakind_attnum = Anum_pg_statistic_stakind1 - 1 + slotidx;
     728        2634 :     staop_attnum = Anum_pg_statistic_staop1 - 1 + slotidx;
     729        2634 :     stacoll_attnum = Anum_pg_statistic_stacoll1 - 1 + slotidx;
     730             : 
     731        2634 :     if (DatumGetInt16(values[stakind_attnum]) != stakind)
     732             :     {
     733        2622 :         values[stakind_attnum] = Int16GetDatum(stakind);
     734        2622 :         replaces[stakind_attnum] = true;
     735             :     }
     736        2634 :     if (DatumGetObjectId(values[staop_attnum]) != staop)
     737             :     {
     738        2592 :         values[staop_attnum] = ObjectIdGetDatum(staop);
     739        2592 :         replaces[staop_attnum] = true;
     740             :     }
     741        2634 :     if (DatumGetObjectId(values[stacoll_attnum]) != stacoll)
     742             :     {
     743         686 :         values[stacoll_attnum] = ObjectIdGetDatum(stacoll);
     744         686 :         replaces[stacoll_attnum] = true;
     745             :     }
     746        2634 :     if (!stanumbers_isnull)
     747             :     {
     748        1952 :         values[Anum_pg_statistic_stanumbers1 - 1 + slotidx] = stanumbers;
     749        1952 :         nulls[Anum_pg_statistic_stanumbers1 - 1 + slotidx] = false;
     750        1952 :         replaces[Anum_pg_statistic_stanumbers1 - 1 + slotidx] = true;
     751             :     }
     752        2634 :     if (!stavalues_isnull)
     753             :     {
     754        1402 :         values[Anum_pg_statistic_stavalues1 - 1 + slotidx] = stavalues;
     755        1402 :         nulls[Anum_pg_statistic_stavalues1 - 1 + slotidx] = false;
     756        1402 :         replaces[Anum_pg_statistic_stavalues1 - 1 + slotidx] = true;
     757             :     }
     758        2634 : }
     759             : 
     760             : /*
     761             :  * Upsert the pg_statistic record.
     762             :  */
     763             : static void
     764        1412 : upsert_pg_statistic(Relation starel, HeapTuple oldtup,
     765             :                     Datum *values, bool *nulls, bool *replaces)
     766             : {
     767             :     HeapTuple   newtup;
     768             : 
     769        1412 :     if (HeapTupleIsValid(oldtup))
     770             :     {
     771         120 :         newtup = heap_modify_tuple(oldtup, RelationGetDescr(starel),
     772             :                                    values, nulls, replaces);
     773         120 :         CatalogTupleUpdate(starel, &newtup->t_self, newtup);
     774             :     }
     775             :     else
     776             :     {
     777        1292 :         newtup = heap_form_tuple(RelationGetDescr(starel), values, nulls);
     778        1292 :         CatalogTupleInsert(starel, newtup);
     779             :     }
     780             : 
     781        1412 :     heap_freetuple(newtup);
     782             : 
     783        1412 :     CommandCounterIncrement();
     784        1412 : }
     785             : 
     786             : /*
     787             :  * Delete pg_statistic record.
     788             :  */
     789             : static bool
     790          54 : delete_pg_statistic(Oid reloid, AttrNumber attnum, bool stainherit)
     791             : {
     792          54 :     Relation    sd = table_open(StatisticRelationId, RowExclusiveLock);
     793             :     HeapTuple   oldtup;
     794          54 :     bool        result = false;
     795             : 
     796             :     /* Is there already a pg_statistic tuple for this attribute? */
     797          54 :     oldtup = SearchSysCache3(STATRELATTINH,
     798             :                              ObjectIdGetDatum(reloid),
     799             :                              Int16GetDatum(attnum),
     800             :                              BoolGetDatum(stainherit));
     801             : 
     802          54 :     if (HeapTupleIsValid(oldtup))
     803             :     {
     804          54 :         CatalogTupleDelete(sd, &oldtup->t_self);
     805          54 :         ReleaseSysCache(oldtup);
     806          54 :         result = true;
     807             :     }
     808             : 
     809          54 :     table_close(sd, RowExclusiveLock);
     810             : 
     811          54 :     CommandCounterIncrement();
     812             : 
     813          54 :     return result;
     814             : }
     815             : 
     816             : /*
     817             :  * Initialize values and nulls for a new stats tuple.
     818             :  */
     819             : static void
     820        1292 : init_empty_stats_tuple(Oid reloid, int16 attnum, bool inherited,
     821             :                        Datum *values, bool *nulls, bool *replaces)
     822             : {
     823        1292 :     memset(nulls, true, sizeof(bool) * Natts_pg_statistic);
     824        1292 :     memset(replaces, true, sizeof(bool) * Natts_pg_statistic);
     825             : 
     826             :     /* must initialize non-NULL attributes */
     827             : 
     828        1292 :     values[Anum_pg_statistic_starelid - 1] = ObjectIdGetDatum(reloid);
     829        1292 :     nulls[Anum_pg_statistic_starelid - 1] = false;
     830        1292 :     values[Anum_pg_statistic_staattnum - 1] = Int16GetDatum(attnum);
     831        1292 :     nulls[Anum_pg_statistic_staattnum - 1] = false;
     832        1292 :     values[Anum_pg_statistic_stainherit - 1] = BoolGetDatum(inherited);
     833        1292 :     nulls[Anum_pg_statistic_stainherit - 1] = false;
     834             : 
     835        1292 :     values[Anum_pg_statistic_stanullfrac - 1] = DEFAULT_NULL_FRAC;
     836        1292 :     nulls[Anum_pg_statistic_stanullfrac - 1] = false;
     837        1292 :     values[Anum_pg_statistic_stawidth - 1] = DEFAULT_AVG_WIDTH;
     838        1292 :     nulls[Anum_pg_statistic_stawidth - 1] = false;
     839        1292 :     values[Anum_pg_statistic_stadistinct - 1] = DEFAULT_N_DISTINCT;
     840        1292 :     nulls[Anum_pg_statistic_stadistinct - 1] = false;
     841             : 
     842             :     /* initialize stakind, staop, and stacoll slots */
     843        7752 :     for (int slotnum = 0; slotnum < STATISTIC_NUM_SLOTS; slotnum++)
     844             :     {
     845        6460 :         values[Anum_pg_statistic_stakind1 + slotnum - 1] = (Datum) 0;
     846        6460 :         nulls[Anum_pg_statistic_stakind1 + slotnum - 1] = false;
     847        6460 :         values[Anum_pg_statistic_staop1 + slotnum - 1] = InvalidOid;
     848        6460 :         nulls[Anum_pg_statistic_staop1 + slotnum - 1] = false;
     849        6460 :         values[Anum_pg_statistic_stacoll1 + slotnum - 1] = InvalidOid;
     850        6460 :         nulls[Anum_pg_statistic_stacoll1 + slotnum - 1] = false;
     851             :     }
     852        1292 : }
     853             : 
     854             : /*
     855             :  * Import statistics for a given relation attribute.
     856             :  *
     857             :  * Inserts or replaces a row in pg_statistic for the given relation and
     858             :  * attribute name. It takes input parameters that correspond to columns in the
     859             :  * view pg_stats.
     860             :  *
     861             :  * Parameters null_frac, avg_width, and n_distinct all correspond to NOT NULL
     862             :  * columns in pg_statistic. The remaining parameters all belong to a specific
     863             :  * stakind. Some stakinds require multiple parameters, which must be specified
     864             :  * together (or neither specified).
     865             :  *
     866             :  * Parameters are only superficially validated. Omitting a parameter or
     867             :  * passing NULL leaves the statistic unchanged.
     868             :  *
     869             :  * Parameters corresponding to ANYARRAY columns are instead passed in as text
     870             :  * values, which is a valid input string for an array of the type or element
     871             :  * type of the attribute. Any error generated by the array_in() function will
     872             :  * in turn fail the function.
     873             :  */
     874             : Datum
     875         210 : pg_set_attribute_stats(PG_FUNCTION_ARGS)
     876             : {
     877         210 :     attribute_statistics_update(fcinfo, ERROR);
     878          84 :     PG_RETURN_VOID();
     879             : }
     880             : 
     881             : /*
     882             :  * Delete statistics for the given attribute.
     883             :  */
     884             : Datum
     885          72 : pg_clear_attribute_stats(PG_FUNCTION_ARGS)
     886             : {
     887             :     Oid         reloid;
     888             :     Name        attname;
     889             :     AttrNumber  attnum;
     890             :     bool        inherited;
     891             : 
     892          72 :     stats_check_required_arg(fcinfo, attarginfo, ATTRELATION_ARG);
     893          72 :     reloid = PG_GETARG_OID(ATTRELATION_ARG);
     894             : 
     895          72 :     if (RecoveryInProgress())
     896           0 :         ereport(ERROR,
     897             :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
     898             :                  errmsg("recovery is in progress"),
     899             :                  errhint("Statistics cannot be modified during recovery.")));
     900             : 
     901          72 :     stats_lock_check_privileges(reloid);
     902             : 
     903          66 :     stats_check_required_arg(fcinfo, attarginfo, ATTNAME_ARG);
     904          66 :     attname = PG_GETARG_NAME(ATTNAME_ARG);
     905          66 :     attnum = get_attnum(reloid, NameStr(*attname));
     906             : 
     907          66 :     if (attnum < 0)
     908           6 :         ereport(ERROR,
     909             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     910             :                  errmsg("cannot clear statistics on system column \"%s\"",
     911             :                         NameStr(*attname))));
     912             : 
     913          60 :     if (attnum == InvalidAttrNumber)
     914           6 :         ereport(ERROR,
     915             :                 (errcode(ERRCODE_UNDEFINED_COLUMN),
     916             :                  errmsg("column \"%s\" of relation \"%s\" does not exist",
     917             :                         NameStr(*attname), get_rel_name(reloid))));
     918             : 
     919          54 :     stats_check_required_arg(fcinfo, attarginfo, INHERITED_ARG);
     920          54 :     inherited = PG_GETARG_BOOL(INHERITED_ARG);
     921             : 
     922          54 :     delete_pg_statistic(reloid, attnum, inherited);
     923          54 :     PG_RETURN_VOID();
     924             : }
     925             : 
     926             : Datum
     927        1358 : pg_restore_attribute_stats(PG_FUNCTION_ARGS)
     928             : {
     929        1358 :     LOCAL_FCINFO(positional_fcinfo, NUM_ATTRIBUTE_STATS_ARGS);
     930        1358 :     bool        result = true;
     931             : 
     932        1358 :     InitFunctionCallInfoData(*positional_fcinfo, NULL, NUM_ATTRIBUTE_STATS_ARGS,
     933             :                              InvalidOid, NULL, NULL);
     934             : 
     935        1358 :     if (!stats_fill_fcinfo_from_arg_pairs(fcinfo, positional_fcinfo,
     936             :                                           attarginfo, WARNING))
     937          12 :         result = false;
     938             : 
     939        1358 :     if (!attribute_statistics_update(positional_fcinfo, WARNING))
     940          66 :         result = false;
     941             : 
     942        1328 :     PG_RETURN_BOOL(result);
     943             : }

Generated by: LCOV version 1.14