agora inbox for [email protected]help / color / mirror / Atom feed
[PATCH v27 6/9] Row pattern recognition patch (docs). 267+ messages / 2 participants [nested] [flat]
* [PATCH v27 6/9] Row pattern recognition patch (docs). @ 2024-12-30 23:53 Tatsuo Ishii <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Tatsuo Ishii @ 2024-12-30 23:53 UTC (permalink / raw) --- doc/src/sgml/advanced.sgml | 82 ++++++++++++++++++++++++++++++++++++ doc/src/sgml/func.sgml | 54 ++++++++++++++++++++++++ doc/src/sgml/ref/select.sgml | 38 ++++++++++++++++- 3 files changed, 172 insertions(+), 2 deletions(-) diff --git a/doc/src/sgml/advanced.sgml b/doc/src/sgml/advanced.sgml index 755c9f1485..b0b1d1c51e 100644 --- a/doc/src/sgml/advanced.sgml +++ b/doc/src/sgml/advanced.sgml @@ -537,6 +537,88 @@ WHERE pos < 3; <literal>rank</literal> less than 3. </para> + <para> + Row pattern common syntax can be used to perform row pattern recognition + in a query. The row pattern common syntax includes two sub + clauses: <literal>DEFINE</literal> + and <literal>PATTERN</literal>. <literal>DEFINE</literal> defines + definition variables along with an expression. The expression must be a + logical expression, which means it must + return <literal>TRUE</literal>, <literal>FALSE</literal> + or <literal>NULL</literal>. The expression may comprise column references + and functions. Window functions, aggregate functions and subqueries are + not allowed. An example of <literal>DEFINE</literal> is as follows. + +<programlisting> +DEFINE + LOWPRICE AS price <= 100, + UP AS price > PREV(price), + DOWN AS price < PREV(price) +</programlisting> + + Note that <function>PREV</function> returns the price column in the + previous row if it's called in a context of row pattern recognition. Thus in + the second line the definition variable "UP" is <literal>TRUE</literal> + when the price column in the current row is greater than the price column + in the previous row. Likewise, "DOWN" is <literal>TRUE</literal> when when + the price column in the current row is lower than the price column in the + previous row. + </para> + <para> + Once <literal>DEFINE</literal> exists, <literal>PATTERN</literal> can be + used. <literal>PATTERN</literal> defines a sequence of rows that satisfies + certain conditions. For example following <literal>PATTERN</literal> + defines that a row starts with the condition "LOWPRICE", then one or more + rows satisfy "UP" and finally one or more rows satisfy "DOWN". Note that + "+" means one or more matches. Also you can use "*", which means zero or + more matches. If a sequence of rows which satisfies the PATTERN is found, + in the starting row of the sequence of rows all window functions and + aggregates are shown in the target list. Note that aggregations only look + into the matched rows, rather than whole frame. On the second or + subsequent rows all window functions are NULL. Aggregates are NULL or 0 + (count case) depending on its aggregation definition. For rows that do not + match on the PATTERN, all window functions and aggregates are shown AS + NULL too, except count showing 0. This is because the rows do not match, + thus they are in an empty frame. Example of a <literal>SELECT</literal> + using the <literal>DEFINE</literal> and <literal>PATTERN</literal> clause + is as follows. + +<programlisting> +SELECT company, tdate, price, + first_value(price) OVER w, + max(price) OVER w, + count(price) OVER w +FROM stock + WINDOW w AS ( + PARTITION BY company + ORDER BY tdate + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + INITIAL + PATTERN (LOWPRICE UP+ DOWN+) + DEFINE + LOWPRICE AS price <= 100, + UP AS price > PREV(price), + DOWN AS price < PREV(price) +); +</programlisting> +<screen> + company | tdate | price | first_value | max | count +----------+------------+-------+-------------+-----+------- + company1 | 2023-07-01 | 100 | 100 | 200 | 4 + company1 | 2023-07-02 | 200 | | | 0 + company1 | 2023-07-03 | 150 | | | 0 + company1 | 2023-07-04 | 140 | | | 0 + company1 | 2023-07-05 | 150 | | | 0 + company1 | 2023-07-06 | 90 | 90 | 130 | 4 + company1 | 2023-07-07 | 110 | | | 0 + company1 | 2023-07-08 | 130 | | | 0 + company1 | 2023-07-09 | 120 | | | 0 + company1 | 2023-07-10 | 130 | | | 0 +(10 rows) +</screen> + </para> + <para> When a query involves multiple window functions, it is possible to write out each one with a separate <literal>OVER</literal> clause, but this is diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index 47370e581a..17a38c4046 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -23338,6 +23338,7 @@ SELECT count(*) FROM sometable; returns <literal>NULL</literal> if there is no such row. </para></entry> </row> + </tbody> </tgroup> </table> @@ -23377,6 +23378,59 @@ SELECT count(*) FROM sometable; Other frame specifications can be used to obtain other effects. </para> + <para> + Row pattern recognition navigation functions are listed in + <xref linkend="functions-rpr-navigation-table"/>. These functions + can be used to describe DEFINE clause of Row pattern recognition. + </para> + + <table id="functions-rpr-navigation-table"> + <title>Row Pattern Navigation Functions</title> + <tgroup cols="1"> + <thead> + <row> + <entry role="func_table_entry"><para role="func_signature"> + Function + </para> + <para> + Description + </para></entry> + </row> + </thead> + + <tbody> + <row> + <entry role="func_table_entry"><para role="func_signature"> + <indexterm> + <primary>prev</primary> + </indexterm> + <function>prev</function> ( <parameter>value</parameter> <type>anyelement</type> ) + <returnvalue>anyelement</returnvalue> + </para> + <para> + Returns the column value at the previous row; + returns NULL if there is no previous row in the window frame. + </para></entry> + </row> + + <row> + <entry role="func_table_entry"><para role="func_signature"> + <indexterm> + <primary>next</primary> + </indexterm> + <function>next</function> ( <parameter>value</parameter> <type>anyelement</type> ) + <returnvalue>anyelement</returnvalue> + </para> + <para> + Returns the column value at the next row; + returns NULL if there is no next row in the window frame. + </para></entry> + </row> + + </tbody> + </tgroup> + </table> + <note> <para> The SQL standard defines a <literal>RESPECT NULLS</literal> or diff --git a/doc/src/sgml/ref/select.sgml b/doc/src/sgml/ref/select.sgml index d7089eac0b..7e1c9989ba 100644 --- a/doc/src/sgml/ref/select.sgml +++ b/doc/src/sgml/ref/select.sgml @@ -969,8 +969,8 @@ WINDOW <replaceable class="parameter">window_name</replaceable> AS ( <replaceabl The <replaceable class="parameter">frame_clause</replaceable> can be one of <synopsis> -{ RANGE | ROWS | GROUPS } <replaceable>frame_start</replaceable> [ <replaceable>frame_exclusion</replaceable> ] -{ RANGE | ROWS | GROUPS } BETWEEN <replaceable>frame_start</replaceable> AND <replaceable>frame_end</replaceable> [ <replaceable>frame_exclusion</replaceable> ] +{ RANGE | ROWS | GROUPS } <replaceable>frame_start</replaceable> [ <replaceable>frame_exclusion</replaceable> ] [row_pattern_common_syntax] +{ RANGE | ROWS | GROUPS } BETWEEN <replaceable>frame_start</replaceable> AND <replaceable>frame_end</replaceable> [ <replaceable>frame_exclusion</replaceable> ] [row_pattern_common_syntax] </synopsis> where <replaceable>frame_start</replaceable> @@ -1077,6 +1077,40 @@ EXCLUDE NO OTHERS a given peer group will be in the frame or excluded from it. </para> + <para> + The + optional <replaceable class="parameter">row_pattern_common_syntax</replaceable> + defines the <firstterm>row pattern recognition condition</firstterm> for + this + window. <replaceable class="parameter">row_pattern_common_syntax</replaceable> + includes following subclauses. <literal>AFTER MATCH SKIP PAST LAST + ROW</literal> or <literal>AFTER MATCH SKIP TO NEXT ROW</literal> controls + how to proceed to next row position after a match + found. With <literal>AFTER MATCH SKIP PAST LAST ROW</literal> (the + default) next row position is next to the last row of previous match. On + the other hand, with <literal>AFTER MATCH SKIP TO NEXT ROW</literal> next + row position is always next to the last row of previous + match. <literal>DEFINE</literal> defines definition variables along with a + boolean expression. <literal>PATTERN</literal> defines a sequence of rows + that satisfies certain conditions using variables defined + in <literal>DEFINE</literal> clause. If the variable is not defined in + the <literal>DEFINE</literal> clause, it is implicitly assumed + following is defined in the <literal>DEFINE</literal> clause. + +<synopsis> +<literal>variable_name</literal> AS TRUE +</synopsis> + + Note that the maximu number of variables defined + in <literal>DEFINE</literal> clause is 26. + +<synopsis> +[ AFTER MATCH SKIP PAST LAST ROW | AFTER MATCH SKIP TO NEXT ROW ] +PATTERN <replaceable class="parameter">pattern_variable_name</replaceable>[+] [, ...] +DEFINE <replaceable class="parameter">definition_varible_name</replaceable> AS <replaceable class="parameter">expression</replaceable> [, ...] +</synopsis> + </para> + <para> The purpose of a <literal>WINDOW</literal> clause is to specify the behavior of <firstterm>window functions</firstterm> appearing in the query's -- 2.25.1 ----Next_Part(Tue_Dec_31_08_57_07_2024_963)-- Content-Type: Text/X-Patch; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="v27-0007-Row-pattern-recognition-patch-tests.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
* [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt @ 2025-08-28 17:49 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 267+ messages in thread From: Álvaro Herrera @ 2025-08-28 17:49 UTC (permalink / raw) --- src/backend/commands/statscmds.c | 26 +++++----------- src/backend/commands/tablecmds.c | 4 +-- src/backend/parser/parse_utilcmd.c | 13 +++++--- src/backend/tcop/utility.c | 48 +++++++++++++++++------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index e24d540cd45..07b2b5bfef3 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -59,7 +59,7 @@ compare_int16(const void *a, const void *b) * CREATE STATISTICS */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, List *relids) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -92,28 +92,18 @@ CreateStatistics(CreateStatsStmt *stmt) ListCell *cell; ListCell *cell2; - Assert(IsA(stmt, CreateStatsStmt)); - /* * Examine the FROM clause. Currently, we only allow it to be a single * simple table, but later we'll probably allow multiple tables and JOIN - * syntax. The grammar is already prepared for that, so we have to check - * here that what we got is what we can support. + * syntax. Parse analysis checked the list length already, so this is + * just defense-in-depth. */ - if (list_length(stmt->relations) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + Assert(list_length(stmt->relations) == list_length(relids)); + if (list_length(relids) != 1) + elog(ERROR, "only a single relation is allowed in CREATE STATISTICS"); - foreach(cell, stmt->relations) + foreach_oid(relid, relids) { - Node *rln = (Node *) lfirst(cell); - - if (!IsA(rln, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); - /* * CREATE STATISTICS will influence future execution plans but does * not interfere with currently executing plans. So it should be @@ -121,7 +111,7 @@ CreateStatistics(CreateStatsStmt *stmt) * conflicting with ANALYZE and other DDL that sets statistical * information, but not with normal queries. */ - rel = relation_openrv((RangeVar *) rln, ShareUpdateExclusiveLock); + rel = relation_open(relid, ShareUpdateExclusiveLock); /* Restrict to allowed relation types */ if (rel->rd_rel->relkind != RELKIND_RELATION && diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 082a3575d62..1779bae80cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9654,7 +9654,7 @@ ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, /* The CreateStatsStmt has already been through transformStatsStmt */ Assert(stmt->transformed); - address = CreateStatistics(stmt); + address = CreateStatistics(stmt, list_make1_oid(RelationGetRelid(rel))); return address; } @@ -15631,7 +15631,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, } else if (IsA(stmt, CreateStatsStmt)) querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, + transformStatsStmt(list_make1_oid(oldRelId), (CreateStatsStmt *) stmt, cmd)); else diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index afcf54169c3..587e2ef439c 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3138,11 +3138,11 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in relids list (and not on stmt->relations) to determine the + * target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; @@ -3153,6 +3153,11 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) if (stmt->transformed) return stmt; + if (list_length(relids) != 1) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("only a single relation is allowed in CREATE STATISTICS")); + /* Set up pstate */ pstate = make_parsestate(NULL); pstate->p_sourcetext = queryString; @@ -3162,7 +3167,7 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) * to its fields without qualification. Caller is responsible for locking * relation, but we still need to open it. */ - rel = relation_open(relid, NoLock); + rel = relation_open(linitial_oid(relids), NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 4f4191b0ea6..e09da051310 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1867,32 +1867,40 @@ ProcessUtilitySlow(ParseState *pstate, case T_CreateStatsStmt: { - Oid relid; CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); + List *relids = NIL; + ListCell *cell; - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + foreach(cell, stmt->relations) + { + Oid relid; - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); + if (!IsA(lfirst(cell), RangeVar)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create statistics on specified relation"), + errdetail("CREATE STATISTICS only supports tables, materialized views, foreign tables, and partitioned tables.")); + /* + * CREATE STATISTICS will influence future execution + * plans but does not interfere with currently + * executing plans. So it should be enough to take + * ShareUpdateExclusiveLock on relation, conflicting + * with ANALYZE and other DDL that sets statistical + * information, but not with normal queries. + * + * XXX RangeVarCallbackOwnsRelation not needed here, + * to keep the same behavior as before. + */ + relid = RangeVarGetRelid(castNode(RangeVar, lfirst(cell)), + ShareUpdateExclusiveLock, false); + relids = lappend_oid(relids, relid); + } /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); + stmt = transformStatsStmt(relids, stmt, queryString); - address = CreateStatistics(stmt); + /* ... and execute the command */ + address = CreateStatistics(stmt, relids); } break; diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..7b8cb40cd83 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, List *relids); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 9f2b58de797..28165eb1198 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(List *relids, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.39.5 --pbl4mx6qlbtkw6pf Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-CREATE-STATISTICS-Fix-error-message.patch" ^ permalink raw reply [nested|flat] 267+ messages in thread
end of thread, other threads:[~2025-08-28 17:49 UTC | newest] Thread overview: 267+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2024-12-30 23:53 [PATCH v27 6/9] Row pattern recognition patch (docs). Tatsuo Ishii <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]> 2025-08-28 17:49 [PATCH] rewrite ProcessUtilitySlow code for CreateStatsStmt Álvaro Herrera <[email protected]>
This inbox is served by agora; see mirroring instructions for how to clone and mirror all data and code used for this inbox