From 9199be09efcbca1b906b5c41e8524e68b1a6b64e Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Mon, 15 Jan 2024 16:44:55 +0100 Subject: [PATCH v4 1/3] Make stxstattarget nullable To match attstattarget change (commit 4f622503d6d). TODO: catversion --- doc/src/sgml/catalogs.sgml | 28 ++++++++++++------------- doc/src/sgml/ref/alter_statistics.sgml | 13 ++++++------ src/backend/commands/statscmds.c | 21 ++++++++++++++++--- src/backend/parser/gram.y | 4 ++-- src/backend/statistics/extended_stats.c | 4 +++- src/bin/pg_dump/pg_dump.c | 10 +++++---- src/bin/psql/describe.c | 3 ++- src/include/catalog/pg_statistic_ext.h | 6 +++--- src/include/nodes/parsenodes.h | 2 +- 9 files changed, 56 insertions(+), 35 deletions(-) diff --git a/doc/src/sgml/catalogs.sgml b/doc/src/sgml/catalogs.sgml index c15d861e823..34458df6d4c 100644 --- a/doc/src/sgml/catalogs.sgml +++ b/doc/src/sgml/catalogs.sgml @@ -7633,6 +7633,19 @@ <structname>pg_statistic_ext</structname> Columns + + + stxkeys int2vector + (references pg_attribute.attnum) + + + An array of attribute numbers, indicating which table columns are + covered by this statistics object; + for example a value of 1 3 would + mean that the first and the third table columns are covered + + + stxstattarget int2 @@ -7642,7 +7655,7 @@ <structname>pg_statistic_ext</structname> Columns of statistics accumulated for this statistics object by ANALYZE. A zero value indicates that no statistics should be collected. - A negative value says to use the maximum of the statistics targets of + A null value says to use the maximum of the statistics targets of the referenced columns, if set, or the system default statistics target. Positive values of stxstattarget determine the target number of most common values @@ -7650,19 +7663,6 @@ <structname>pg_statistic_ext</structname> Columns - - - stxkeys int2vector - (references pg_attribute.attnum) - - - An array of attribute numbers, indicating which table columns are - covered by this statistics object; - for example a value of 1 3 would - mean that the first and the third table columns are covered - - - stxkind char[] diff --git a/doc/src/sgml/ref/alter_statistics.sgml b/doc/src/sgml/ref/alter_statistics.sgml index 73cc9e830de..c16caa0b61b 100644 --- a/doc/src/sgml/ref/alter_statistics.sgml +++ b/doc/src/sgml/ref/alter_statistics.sgml @@ -26,7 +26,7 @@ ALTER STATISTICS name OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER } ALTER STATISTICS name RENAME TO new_name ALTER STATISTICS name SET SCHEMA new_schema -ALTER STATISTICS name SET STATISTICS new_target +ALTER STATISTICS name SET STATISTICS { integer | DEFAULT } @@ -96,15 +96,16 @@ Parameters - new_target + SET STATISTICS { integer | DEFAULT } The statistic-gathering target for this statistics object for subsequent ANALYZE operations. - The target can be set in the range 0 to 10000; alternatively, set it - to -1 to revert to using the maximum of the statistics target of the - referenced columns, if set, or the system default statistics - target (). + The target can be set in the range 0 to 10000. Set it to + DEFAULT to revert to using the system default + statistics target (). + (Setting to a value of -1 is an obsolete way spelling to get the same + outcome.) For more information on the use of statistics by the PostgreSQL query planner, refer to . diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index b1a9c74bd63..b2e509afef0 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -498,9 +498,9 @@ CreateStatistics(CreateStatsStmt *stmt) values[Anum_pg_statistic_ext_stxrelid - 1] = ObjectIdGetDatum(relid); values[Anum_pg_statistic_ext_stxname - 1] = NameGetDatum(&stxname); values[Anum_pg_statistic_ext_stxnamespace - 1] = ObjectIdGetDatum(namespaceId); - values[Anum_pg_statistic_ext_stxstattarget - 1] = Int16GetDatum(-1); values[Anum_pg_statistic_ext_stxowner - 1] = ObjectIdGetDatum(stxowner); values[Anum_pg_statistic_ext_stxkeys - 1] = PointerGetDatum(stxkeys); + nulls[Anum_pg_statistic_ext_stxstattarget - 1] = true; values[Anum_pg_statistic_ext_stxkind - 1] = PointerGetDatum(stxkind); values[Anum_pg_statistic_ext_stxexprs - 1] = exprsDatum; @@ -609,7 +609,19 @@ AlterStatistics(AlterStatsStmt *stmt) bool repl_null[Natts_pg_statistic_ext]; bool repl_repl[Natts_pg_statistic_ext]; ObjectAddress address; - int newtarget = stmt->stxstattarget; + int newtarget; + + if (stmt->stxstattarget) + { + newtarget = intVal(stmt->stxstattarget); + } + else + { + /* + * -1 was used in previous versions to represent the default setting + */ + newtarget = -1; + } /* Limit statistics target to a sane range */ if (newtarget < -1) @@ -676,7 +688,10 @@ AlterStatistics(AlterStatsStmt *stmt) /* replace the stxstattarget column */ repl_repl[Anum_pg_statistic_ext_stxstattarget - 1] = true; - repl_val[Anum_pg_statistic_ext_stxstattarget - 1] = Int16GetDatum(newtarget); + if (newtarget != -1) + repl_val[Anum_pg_statistic_ext_stxstattarget - 1] = Int16GetDatum(newtarget); + else + repl_null[Anum_pg_statistic_ext_stxstattarget - 1] = true; newtup = heap_modify_tuple(oldtup, RelationGetDescr(rel), repl_val, repl_null, repl_repl); diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index 3460fea56ba..e0e30ce1716 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -4597,7 +4597,7 @@ stats_param: ColId *****************************************************************************/ AlterStatsStmt: - ALTER STATISTICS any_name SET STATISTICS SignedIconst + ALTER STATISTICS any_name SET STATISTICS set_statistics_value { AlterStatsStmt *n = makeNode(AlterStatsStmt); @@ -4606,7 +4606,7 @@ AlterStatsStmt: n->stxstattarget = $6; $$ = (Node *) n; } - | ALTER STATISTICS IF_P EXISTS any_name SET STATISTICS SignedIconst + | ALTER STATISTICS IF_P EXISTS any_name SET STATISTICS set_statistics_value { AlterStatsStmt *n = makeNode(AlterStatsStmt); diff --git a/src/backend/statistics/extended_stats.c b/src/backend/statistics/extended_stats.c index c5461514d8f..33f1ad98d49 100644 --- a/src/backend/statistics/extended_stats.c +++ b/src/backend/statistics/extended_stats.c @@ -457,13 +457,15 @@ fetch_statentries_for_relation(Relation pg_statext, Oid relid) entry->statOid = staForm->oid; entry->schema = get_namespace_name(staForm->stxnamespace); entry->name = pstrdup(NameStr(staForm->stxname)); - entry->stattarget = staForm->stxstattarget; for (i = 0; i < staForm->stxkeys.dim1; i++) { entry->columns = bms_add_member(entry->columns, staForm->stxkeys.values[i]); } + datum = SysCacheGetAttr(STATEXTOID, htup, Anum_pg_statistic_ext_stxstattarget, &isnull); + entry->stattarget = isnull ? -1 : DatumGetInt16(datum); + /* decode the stxkind char array into a list of chars */ datum = SysCacheGetAttrNotNull(STATEXTOID, htup, Anum_pg_statistic_ext_stxkind); diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index bc20a025ce4..b151eac6654 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -7542,7 +7542,7 @@ getExtendedStatistics(Archive *fout) if (fout->remoteVersion < 130000) appendPQExpBufferStr(query, "SELECT tableoid, oid, stxname, " - "stxnamespace, stxowner, stxrelid, (-1) AS stxstattarget " + "stxnamespace, stxowner, stxrelid, NULL AS stxstattarget " "FROM pg_catalog.pg_statistic_ext"); else appendPQExpBufferStr(query, "SELECT tableoid, oid, stxname, " @@ -7575,7 +7575,10 @@ getExtendedStatistics(Archive *fout) statsextinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_stxowner)); statsextinfo[i].stattable = findTableByOid(atooid(PQgetvalue(res, i, i_stxrelid))); - statsextinfo[i].stattarget = atoi(PQgetvalue(res, i, i_stattarget)); + if (PQgetisnull(res, i, i_stattarget)) + statsextinfo[i].stattarget = -1; + else + statsextinfo[i].stattarget = atoi(PQgetvalue(res, i, i_stattarget)); /* Decide whether we want to dump it */ selectDumpableStatisticsObject(&(statsextinfo[i]), fout); @@ -17010,8 +17013,7 @@ dumpStatisticsExt(Archive *fout, const StatsExtInfo *statsextinfo) /* * We only issue an ALTER STATISTICS statement if the stxstattarget entry - * for this statistics object is non-negative (i.e. it's not the default - * value). + * for this statistics object is not the default value. */ if (statsextinfo->stattarget >= 0) { diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index 37f95163201..7be1ffba1ee 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -2792,7 +2792,8 @@ describeOneTableDetails(const char *schemaname, PQgetvalue(result, i, 1)); /* Show the stats target if it's not default */ - if (strcmp(PQgetvalue(result, i, 8), "-1") != 0) + if (!PQgetisnull(result, i, 8) && + strcmp(PQgetvalue(result, i, 8), "-1") != 0) appendPQExpBuffer(&buf, "; STATISTICS %s", PQgetvalue(result, i, 8)); diff --git a/src/include/catalog/pg_statistic_ext.h b/src/include/catalog/pg_statistic_ext.h index 104b7db1f95..1ef58b33e3e 100644 --- a/src/include/catalog/pg_statistic_ext.h +++ b/src/include/catalog/pg_statistic_ext.h @@ -43,15 +43,15 @@ CATALOG(pg_statistic_ext,3381,StatisticExtRelationId) * object's namespace */ Oid stxowner BKI_LOOKUP(pg_authid); /* statistics object's owner */ - int16 stxstattarget BKI_DEFAULT(-1); /* statistics target */ /* - * variable-length fields start here, but we allow direct access to - * stxkeys + * variable-length/nullable fields start here, but we allow direct access + * to stxkeys */ int2vector stxkeys BKI_FORCE_NOT_NULL; /* array of column keys */ #ifdef CATALOG_VARLEN + int16 stxstattarget BKI_DEFAULT(_null_) BKI_FORCE_NULL; /* statistics target */ char stxkind[1] BKI_FORCE_NOT_NULL; /* statistics kinds requested * to build */ pg_node_tree stxexprs; /* A list of expression trees for stats diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index b3181f34aee..59ea2f0a9f0 100644 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -3267,7 +3267,7 @@ typedef struct AlterStatsStmt { NodeTag type; List *defnames; /* qualified name (list of String) */ - int stxstattarget; /* statistics target */ + Node *stxstattarget; /* statistics target */ bool missing_ok; /* skip error if statistics object is missing */ } AlterStatsStmt; base-commit: 31acee4b66f9f88ad5c19c1276252688bdaa95c9 -- 2.43.0