agora inbox for [email protected]help / color / mirror / Atom feed
Re: Amcheck: do rightlink verification with lock coupling 273+ messages / 4 participants [nested] [flat]
* Re: Amcheck: do rightlink verification with lock coupling @ 2020-01-11 01:45 Tomas Vondra <[email protected]> 0 siblings, 1 reply; 273+ messages in thread From: Tomas Vondra @ 2020-01-11 01:45 UTC (permalink / raw) To: Alvaro Herrera <[email protected]>; +Cc: Andrey Borodin <[email protected]>; pgsql-hackers; Peter Geoghegan <[email protected]> On Thu, Sep 12, 2019 at 10:16:12AM -0300, Alvaro Herrera wrote: >On 2019-Sep-12, Andrey Borodin wrote: > >> This patch violates one of amcheck design principles: current code >> does not ever take more than one page lock. I do not know: should we >> hold this rule or should we use more deep check? > >The check does seem worthwhile to me. > >As far as I know, in btree you can lock the right sibling of a page >while keeping lock on the page itself, without risk of deadlock. So I'm >not sure what's the issue with that. This is in the README: > > In most cases we release our lock and pin on a page before attempting > to acquire pin and lock on the page we are moving to. In a few places > it is necessary to lock the next page before releasing the current one. > This is safe when moving right or up, but not when moving left or down > (else we'd create the possibility of deadlocks). > >I suppose Peter was concerned about being able to run amcheck without >causing any trouble at all for concurrent operation; maybe we can retain >that property by making this check optional. > Peter, any opinion on this proposed amcheck patch? In the other thread [1] you seemed to agree this is worth checking, and Alvaro's proposal to make this check optional seems like a reasonable compromise with respect to the locking. [1] https://www.postgresql.org/message-id/flat/[email protected] -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services ^ permalink raw reply [nested|flat] 273+ messages in thread
* Re: Amcheck: do rightlink verification with lock coupling @ 2020-01-11 02:49 Peter Geoghegan <[email protected]> parent: Tomas Vondra <[email protected]> 0 siblings, 2 replies; 273+ messages in thread From: Peter Geoghegan @ 2020-01-11 02:49 UTC (permalink / raw) To: Tomas Vondra <[email protected]>; +Cc: Alvaro Herrera <[email protected]>; Andrey Borodin <[email protected]>; pgsql-hackers On Fri, Jan 10, 2020 at 5:45 PM Tomas Vondra <[email protected]> wrote: > Peter, any opinion on this proposed amcheck patch? In the other thread > [1] you seemed to agree this is worth checking, and Alvaro's proposal to > make this check optional seems like a reasonable compromise with respect > to the locking. It's a good idea, and it probably doesn't even need to be made optional -- lock coupling to the right is safe on a primary, and should also be safe on standbys (though I should triple check the REDO routines to be sure). The patch only does lock coupling when it proves necessary, which ought to only happen when there is a concurrent page split, which ought to be infrequent. Maybe there is no need to compromise. I'm curious why Andrey's corruption problems were not detected by the cross-page amcheck test, though. We compare the first non-pivot tuple on the right sibling leaf page with the last one on the target page, towards the end of bt_target_page_check() -- isn't that almost as good as what you have here in practice? I probably would have added something like this myself earlier, if I had reason to think that verification would be a lot more effective that way. To be clear, I believe that Andrey wrote this patch for a reason -- I assume that it makes a noticeable and consistent difference. I would like to gain a better understanding of why that was for my own benefit, though. For example, it might be that page deletion was a factor that made the test I mentioned less effective. I care about the specifics. -- Peter Geoghegan ^ permalink raw reply [nested|flat] 273+ messages in thread
* Re: Amcheck: do rightlink verification with lock coupling @ 2020-01-11 12:25 Tomas Vondra <[email protected]> parent: Peter Geoghegan <[email protected]> 1 sibling, 1 reply; 273+ messages in thread From: Tomas Vondra @ 2020-01-11 12:25 UTC (permalink / raw) To: Peter Geoghegan <[email protected]>; +Cc: Alvaro Herrera <[email protected]>; Andrey Borodin <[email protected]>; pgsql-hackers On Fri, Jan 10, 2020 at 06:49:33PM -0800, Peter Geoghegan wrote: >On Fri, Jan 10, 2020 at 5:45 PM Tomas Vondra ><[email protected]> wrote: >> Peter, any opinion on this proposed amcheck patch? In the other thread >> [1] you seemed to agree this is worth checking, and Alvaro's proposal to >> make this check optional seems like a reasonable compromise with respect >> to the locking. > >It's a good idea, and it probably doesn't even need to be made >optional -- lock coupling to the right is safe on a primary, and >should also be safe on standbys (though I should triple check the REDO >routines to be sure). The patch only does lock coupling when it proves >necessary, which ought to only happen when there is a concurrent page >split, which ought to be infrequent. Maybe there is no need to >compromise. > OK, that makes sense. >I'm curious why Andrey's corruption problems were not detected by the >cross-page amcheck test, though. We compare the first non-pivot tuple >on the right sibling leaf page with the last one on the target page, >towards the end of bt_target_page_check() -- isn't that almost as good >as what you have here in practice? I probably would have added >something like this myself earlier, if I had reason to think that >verification would be a lot more effective that way. > >To be clear, I believe that Andrey wrote this patch for a reason -- I >assume that it makes a noticeable and consistent difference. I would >like to gain a better understanding of why that was for my own >benefit, though. For example, it might be that page deletion was a >factor that made the test I mentioned less effective. I care about the >specifics. > Understood. Is that a reason to not commit of this patch now, though? regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services ^ permalink raw reply [nested|flat] 273+ messages in thread
* Re: Amcheck: do rightlink verification with lock coupling @ 2020-01-13 23:49 Peter Geoghegan <[email protected]> parent: Tomas Vondra <[email protected]> 0 siblings, 1 reply; 273+ messages in thread From: Peter Geoghegan @ 2020-01-13 23:49 UTC (permalink / raw) To: Tomas Vondra <[email protected]>; +Cc: Alvaro Herrera <[email protected]>; Andrey Borodin <[email protected]>; pgsql-hackers On Sat, Jan 11, 2020 at 4:25 AM Tomas Vondra <[email protected]> wrote: > Understood. Is that a reason to not commit of this patch now, though? It could use some polishing. Are you interested in committing it? -- Peter Geoghegan ^ permalink raw reply [nested|flat] 273+ messages in thread
* Re: Amcheck: do rightlink verification with lock coupling @ 2020-01-14 01:07 Tomas Vondra <[email protected]> parent: Peter Geoghegan <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Tomas Vondra @ 2020-01-14 01:07 UTC (permalink / raw) To: Peter Geoghegan <[email protected]>; +Cc: Alvaro Herrera <[email protected]>; Andrey Borodin <[email protected]>; pgsql-hackers On Mon, Jan 13, 2020 at 03:49:40PM -0800, Peter Geoghegan wrote: >On Sat, Jan 11, 2020 at 4:25 AM Tomas Vondra ><[email protected]> wrote: >> Understood. Is that a reason to not commit of this patch now, though? > >It could use some polishing. Are you interested in committing it? > Not really - as a CFM I was trying to revive patches that seem in good shape but not moving. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services ^ permalink raw reply [nested|flat] 273+ messages in thread
* Re: Amcheck: do rightlink verification with lock coupling @ 2020-01-14 04:47 Andrey Borodin <[email protected]> parent: Peter Geoghegan <[email protected]> 1 sibling, 2 replies; 273+ messages in thread From: Andrey Borodin @ 2020-01-14 04:47 UTC (permalink / raw) To: Peter Geoghegan <[email protected]>; +Cc: Tomas Vondra <[email protected]>; Alvaro Herrera <[email protected]>; pgsql-hackers Hi Peter! Sorry for answering so long. > 11 янв. 2020 г., в 7:49, Peter Geoghegan <[email protected]> написал(а): > > I'm curious why Andrey's corruption problems were not detected by the > cross-page amcheck test, though. We compare the first non-pivot tuple > on the right sibling leaf page with the last one on the target page, > towards the end of bt_target_page_check() -- isn't that almost as good > as what you have here in practice? I probably would have added > something like this myself earlier, if I had reason to think that > verification would be a lot more effective that way. We were dealing with corruption caused by lost page update. Consider two pages: A->B If A is split into A` and C we get: A`->C->B But if update of A is lost we still have A->B, but B backward pointers points to C. B's smallest key is bigger than hikey of A`, but this do not violate cross-pages invariant. Page updates may be lost due to bug in backup software with incremental backups, bug in storage layer of Aurora-style system, bug in page cache, incorrect fsync error handling, bug in ssd firmware etc. And our data checksums do not detect this kind of corruption. BTW I think that it would be better if our checksums were not stored on a page itseft, they could detect this kind of faults. We were able to timely detect all those problems on primaries in our testing environment. But much later found out that some standbys were corrupted, the problem appeared only when they were promoted. Also, in nearby thread Grygory Rylov (g0djan) is trying to enable one more invariant in standby checks. Thanks! Best regards, Andrey Borodin. ^ permalink raw reply [nested|flat] 273+ messages in thread
* Re: Amcheck: do rightlink verification with lock coupling @ 2020-01-16 09:50 Andrey Borodin <[email protected]> parent: Andrey Borodin <[email protected]> 1 sibling, 0 replies; 273+ messages in thread From: Andrey Borodin @ 2020-01-16 09:50 UTC (permalink / raw) To: Peter Geoghegan <[email protected]>; +Cc: Tomas Vondra <[email protected]>; Alvaro Herrera <[email protected]>; pgsql-hackers > 14 янв. 2020 г., в 9:47, Andrey Borodin <[email protected]> написал(а): > > Page updates may be lost due to bug in backup software with incremental > backups, bug in storage layer of Aurora-style system, bug in page cache, incorrect > fsync error handling, bug in ssd firmware etc. And our data checksums do not > detect this kind of corruption. BTW I think that it would be better if our > checksums were not stored on a page itseft, they could detect this kind of faults. Observed it just now. There is one HA cluster where a node was marked dead. This node was disconnected from cluster, but due to human error there was postgres running. Node managed to install block-level incremental backup to the chain. And backup software did not detect that backup step was taken from part of timeline that was not in actual timeline's history. Result of restoration is: man-w%/%db R # select bt_index_check('%.pk_%'); bt_index_check ---------------- (1 row) Time: 1411.065 ms (00:01.411) man-w%/%db R # select patched_index_check('%.pk_%'); ERROR: XX002: left link/right link pair in index "pk_labels" not in agreement DETAIL: Block=42705 left block=42707 left link from block=45495. LOCATION: bt_recheck_block_rightlink, verify_nbtree.c:621 Time: 671.336 ms ('%' is replacing removed chars) I understand that this corruption was not introduced by postgres itself, but by combination of bug in two 3rd party tools and human error. But I can imagine similar corruptions with different root causes. Best regards, Andrey Borodin. ^ permalink raw reply [nested|flat] 273+ messages in thread
* Re: Amcheck: do rightlink verification with lock coupling @ 2020-01-17 01:11 Peter Geoghegan <[email protected]> parent: Andrey Borodin <[email protected]> 1 sibling, 1 reply; 273+ messages in thread From: Peter Geoghegan @ 2020-01-17 01:11 UTC (permalink / raw) To: Andrey Borodin <[email protected]>; +Cc: Tomas Vondra <[email protected]>; Alvaro Herrera <[email protected]>; pgsql-hackers On Mon, Jan 13, 2020 at 8:47 PM Andrey Borodin <[email protected]> wrote: > > 11 янв. 2020 г., в 7:49, Peter Geoghegan <[email protected]> написал(а): > > I'm curious why Andrey's corruption problems were not detected by the > > cross-page amcheck test, though. We compare the first non-pivot tuple > > on the right sibling leaf page with the last one on the target page, > > towards the end of bt_target_page_check() -- isn't that almost as good > > as what you have here in practice? I probably would have added > > something like this myself earlier, if I had reason to think that > > verification would be a lot more effective that way. > > We were dealing with corruption caused by lost page update. Consider two pages: > A->B > If A is split into A` and C we get: > A`->C->B > But if update of A is lost we still have > A->B, but B backward pointers points to C. > B's smallest key is bigger than hikey of A`, but this do not violate > cross-pages invariant. > > Page updates may be lost due to bug in backup software with incremental > backups, bug in storage layer of Aurora-style system, bug in page cache, incorrect > fsync error handling, bug in ssd firmware etc. And our data checksums do not > detect this kind of corruption. BTW I think that it would be better if our > checksums were not stored on a page itseft, they could detect this kind of faults. I find this argument convincing. I'll try to get this committed soon. While you could have used bt_index_parent_check() or heapallindexed to detect the issue, those two options are a lot more expensive (plus the former option won't work on a standby). Relaxing the principle that says that we shouldn't hold multiple buffer locks concurrently doesn't seem like that much to ask for to get such a clear benefit. I think that this is safe, but page deletion/half-dead pages need more thought. In general, the target page could have become "ignorable" when rechecked. > We were able to timely detect all those problems on primaries in our testing > environment. But much later found out that some standbys were corrupted, > the problem appeared only when they were promoted. > Also, in nearby thread Grygory Rylov (g0djan) is trying to enable one more > invariant in standby checks. I looked at that thread just now, but Grygory didn't say why this true root check was particularly important, so I can't see much upside. Plus that seems riskier than what you have in mind here. Does it have something to do with the true root looking like a deleted page? The details matter. -- Peter Geoghegan ^ permalink raw reply [nested|flat] 273+ messages in thread
* Re: Amcheck: do rightlink verification with lock coupling @ 2020-01-18 01:43 Peter Geoghegan <[email protected]> parent: Peter Geoghegan <[email protected]> 0 siblings, 1 reply; 273+ messages in thread From: Peter Geoghegan @ 2020-01-18 01:43 UTC (permalink / raw) To: Andrey Borodin <[email protected]>; +Cc: Tomas Vondra <[email protected]>; Alvaro Herrera <[email protected]>; pgsql-hackers On Thu, Jan 16, 2020 at 5:11 PM Peter Geoghegan <[email protected]> wrote: > I find this argument convincing. I'll try to get this committed soon. > > While you could have used bt_index_parent_check() or heapallindexed to > detect the issue, those two options are a lot more expensive (plus the > former option won't work on a standby). Relaxing the principle that > says that we shouldn't hold multiple buffer locks concurrently doesn't > seem like that much to ask for to get such a clear benefit. Having looked into it some more, I now have my doubts about this patch. REDO routine code like btree_xlog_split() and btree_xlog_unlink_page() feel entitled to only lock one page at a time, which invalidates the assumption that we can couple locks on the leaf level to verify mutual agreement in left and right sibling links (with only an AccessShareLock on bt_index_check()'s target index relation). It would definitely be safe for bt_index_check() to so this were it not running in recovery mode, but that doesn't seem very useful on its own. I tried to come up with a specific example of how this could be unsafe, but my explanation was all over the place (this could have had something to do with it being Friday evening). Even still, it's up to the patch to justify why it's safe, and that seems even more difficult. -- Peter Geoghegan ^ permalink raw reply [nested|flat] 273+ messages in thread
* Re: Amcheck: do rightlink verification with lock coupling @ 2020-01-22 19:59 Peter Geoghegan <[email protected]> parent: Peter Geoghegan <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Peter Geoghegan @ 2020-01-22 19:59 UTC (permalink / raw) To: Andrey Borodin <[email protected]>; +Cc: Tomas Vondra <[email protected]>; Alvaro Herrera <[email protected]>; pgsql-hackers On Fri, Jan 17, 2020 at 5:43 PM Peter Geoghegan <[email protected]> wrote: > I tried to come up with a specific example of how this could be > unsafe, but my explanation was all over the place (this could have had > something to do with it being Friday evening). Even still, it's up to > the patch to justify why it's safe, and that seems even more > difficult. I can't see a way around this problem, so I'm marking the patch rejected. Thanks -- Peter Geoghegan ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
* [PATCH v4] Restructure CreateStatsStmt parse-analysis processing @ 2025-10-17 11:55 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 273+ messages in thread From: Álvaro Herrera @ 2025-10-17 11:55 UTC (permalink / raw) Previously, a lot of code would be run in the ProcessUtility pipeline, where it is not welcome (that's supposed to be just a giant dispatcher switch). Move the parse analysis code to CreateStatistics() instead, which then means we don't need to open the relation twice; it also allows us to give a better error message when something other than a relation name is given in the FROM clause. It also means we no longer need to do the transform step in ATPostAlterTypeParse(), which saves a bit of code. Author: Jian He <[email protected]> Discussion: https://postgr.es/m/CACJufxFcvo=fM6J9RC88n1U=eS9+5QdftwUNQMu5h7OgjmqDwQ@mail.gmail.com --- src/backend/commands/statscmds.c | 13 +++++++++++-- src/backend/commands/tablecmds.c | 16 +--------------- src/backend/parser/parse_utilcmd.c | 13 +++---------- src/backend/tcop/utility.c | 29 +---------------------------- src/include/commands/defrem.h | 2 +- src/include/parser/parse_utilcmd.h | 2 +- 6 files changed, 18 insertions(+), 57 deletions(-) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 27bf67e7c4b..632228c13ed 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" +#include "parser/parse_utilcmd.h" #include "statistics/statistics.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -58,9 +59,14 @@ compare_int16(const void *a, const void *b) /* * CREATE STATISTICS + * + * queryString is the source text of the CREATE STATISTICS command, if any. + * + * Normally stmt has not already undergone transformation, but in some cases + * it might have, such as CREATE TABLE (LIKE). */ ObjectAddress -CreateStatistics(CreateStatsStmt *stmt) +CreateStatistics(CreateStatsStmt *stmt, const char *queryString) { int16 attnums[STATS_MAX_DIMENSIONS]; int nattnums = 0; @@ -113,7 +119,7 @@ CreateStatistics(CreateStatsStmt *stmt) if (!IsA(rln, RangeVar)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("only a single relation is allowed in CREATE STATISTICS"))); + errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); /* * CREATE STATISTICS will influence future execution plans but does @@ -151,6 +157,9 @@ CreateStatistics(CreateStatsStmt *stmt) Assert(rel); relid = RelationGetRelid(rel); + /* Run parse analysis */ + stmt = transformStatsStmt(rel, stmt, queryString); + /* * If the node has a name, split it up and determine creation namespace. * If not, put the object in the same namespace as the relation, and cons diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5fd8b51312c..a1732b71e55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9650,16 +9650,7 @@ static ObjectAddress ATExecAddStatistics(AlteredTableInfo *tab, Relation rel, CreateStatsStmt *stmt, bool is_rebuild, LOCKMODE lockmode) { - ObjectAddress address; - - Assert(IsA(stmt, CreateStatsStmt)); - - /* The CreateStatsStmt has already been through transformStatsStmt */ - Assert(stmt->transformed); - - address = CreateStatistics(stmt); - - return address; + return CreateStatistics(stmt, NULL); } /* @@ -15632,11 +15623,6 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, querytree_list = lappend(querytree_list, stmt); querytree_list = list_concat(querytree_list, afterStmts); } - else if (IsA(stmt, CreateStatsStmt)) - querytree_list = lappend(querytree_list, - transformStatsStmt(oldRelId, - (CreateStatsStmt *) stmt, - cmd)); else querytree_list = lappend(querytree_list, stmt); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..89c8315117b 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3137,16 +3137,14 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) * transformStatsStmt - parse analysis for CREATE STATISTICS * * To avoid race conditions, it's important that this function relies only on - * the passed-in relid (and not on stmt->relation) to determine the target - * relation. + * the passed-in rel (and not on stmt->relation) as the target relation. */ CreateStatsStmt * -transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) +transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString) { ParseState *pstate; ParseNamespaceItem *nsitem; ListCell *l; - Relation rel; /* Nothing to do if statement already transformed. */ if (stmt->transformed) @@ -3158,10 +3156,8 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) /* * Put the parent table into the rtable so that the expressions can refer - * to its fields without qualification. Caller is responsible for locking - * relation, but we still need to open it. + * to its fields without qualification. */ - rel = relation_open(relid, NoLock); nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, true); @@ -3196,9 +3192,6 @@ transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString) free_parsestate(pstate); - /* Close relation */ - table_close(rel, NoLock); - /* Mark statement as successfully transformed */ stmt->transformed = true; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 918db53dd5e..027356f6e77 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1866,34 +1866,7 @@ ProcessUtilitySlow(ParseState *pstate, break; case T_CreateStatsStmt: - { - Oid relid; - CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree; - RangeVar *rel = (RangeVar *) linitial(stmt->relations); - - if (!IsA(rel, RangeVar)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("CREATE STATISTICS only supports relation names in the FROM clause"))); - - /* - * CREATE STATISTICS will influence future execution plans - * but does not interfere with currently executing plans. - * So it should be enough to take ShareUpdateExclusiveLock - * on relation, conflicting with ANALYZE and other DDL - * that sets statistical information, but not with normal - * queries. - * - * XXX RangeVarCallbackOwnsRelation not needed here, to - * keep the same behavior as before. - */ - relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false); - - /* Run parse analysis ... */ - stmt = transformStatsStmt(relid, stmt, queryString); - - address = CreateStatistics(stmt); - } + address = CreateStatistics((CreateStatsStmt *) parsetree, queryString); break; case T_AlterStatsStmt: diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index dd22b5efdfd..684f62109fd 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -85,7 +85,7 @@ extern void RemoveOperatorById(Oid operOid); extern ObjectAddress AlterOperator(AlterOperatorStmt *stmt); /* commands/statscmds.c */ -extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt); +extern ObjectAddress CreateStatistics(CreateStatsStmt *stmt, const char *queryString); extern ObjectAddress AlterStatistics(AlterStatsStmt *stmt); extern void RemoveStatisticsById(Oid statsOid); extern void RemoveStatisticsDataById(Oid statsOid, bool inh); diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..67255799f0a 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -26,7 +26,7 @@ extern AlterTableStmt *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, List **afterStmts); extern IndexStmt *transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString); -extern CreateStatsStmt *transformStatsStmt(Oid relid, CreateStatsStmt *stmt, +extern CreateStatsStmt *transformStatsStmt(Relation rel, CreateStatsStmt *stmt, const char *queryString); extern void transformRuleStmt(RuleStmt *stmt, const char *queryString, List **actions, Node **whereClause); -- 2.47.3 --xzcma6eafn4gmiua-- ^ permalink raw reply [nested|flat] 273+ messages in thread
end of thread, other threads:[~2025-10-17 11:55 UTC | newest] Thread overview: 273+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2020-01-11 01:45 Re: Amcheck: do rightlink verification with lock coupling Tomas Vondra <[email protected]> 2020-01-11 02:49 ` Peter Geoghegan <[email protected]> 2020-01-11 12:25 ` Tomas Vondra <[email protected]> 2020-01-13 23:49 ` Peter Geoghegan <[email protected]> 2020-01-14 01:07 ` Tomas Vondra <[email protected]> 2020-01-14 04:47 ` Andrey Borodin <[email protected]> 2020-01-16 09:50 ` Andrey Borodin <[email protected]> 2020-01-17 01:11 ` Peter Geoghegan <[email protected]> 2020-01-18 01:43 ` Peter Geoghegan <[email protected]> 2020-01-22 19:59 ` Peter Geoghegan <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Álvaro Herrera <[email protected]> 2025-10-17 11:55 [PATCH v4] Restructure CreateStatsStmt parse-analysis processing Á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