From 1722d5eb05d8e5d2e064cd1798abcae4f296ca9d Mon Sep 17 00:00:00 2001 From: Amit Langote Date: Thu, 22 May 2025 14:17:24 +0900 Subject: [PATCH] Revert "Don't lock partitions pruned by initial pruning" As pointed out by Tom Lane, the patch introduced fragile and invasive design around plan invalidation handling when locking of prunable partitions was deferred from plancache.c to the executor. In particular, it violated assumptions about CachedPlan immutability and altered executor APIs in ways that are difficult to justify given the added complexity and overhead. This also removes the firstResultRels field added to PlannedStmt in commit 28317de72, which was intended to support deferred locking of certain ModifyTable result relations. Reported-by: Tom Lane Discussion: https://postgr.es/m/605328.1747710381@sss.pgh.pa.us --- contrib/auto_explain/auto_explain.c | 16 +- .../pg_stat_statements/pg_stat_statements.c | 16 +- doc/src/sgml/release-18.sgml | 21 -- src/backend/commands/copyto.c | 5 +- src/backend/commands/createas.c | 5 +- src/backend/commands/explain.c | 22 +- src/backend/commands/extension.c | 4 +- src/backend/commands/matview.c | 5 +- src/backend/commands/portalcmds.c | 1 - src/backend/commands/prepare.c | 9 +- src/backend/commands/trigger.c | 15 -- src/backend/executor/README | 35 +--- src/backend/executor/execMain.c | 127 +---------- src/backend/executor/execParallel.c | 12 +- src/backend/executor/execPartition.c | 66 +----- src/backend/executor/execUtils.c | 1 - src/backend/executor/functions.c | 5 +- src/backend/executor/spi.c | 29 +-- src/backend/optimizer/plan/planner.c | 2 - src/backend/optimizer/plan/setrefs.c | 3 - src/backend/tcop/postgres.c | 4 +- src/backend/tcop/pquery.c | 51 +---- src/backend/utils/cache/plancache.c | 197 +++--------------- src/backend/utils/mmgr/portalmem.c | 4 +- src/include/commands/explain.h | 6 +- src/include/commands/trigger.h | 1 - src/include/executor/execdesc.h | 2 - src/include/executor/executor.h | 34 +-- src/include/nodes/execnodes.h | 3 - src/include/nodes/pathnodes.h | 3 - src/include/nodes/plannodes.h | 7 - src/include/utils/plancache.h | 46 +--- src/include/utils/portal.h | 4 +- 33 files changed, 89 insertions(+), 672 deletions(-) diff --git a/contrib/auto_explain/auto_explain.c b/contrib/auto_explain/auto_explain.c index cd6625020a7..1f4badb4928 100644 --- a/contrib/auto_explain/auto_explain.c +++ b/contrib/auto_explain/auto_explain.c @@ -81,7 +81,7 @@ static ExecutorRun_hook_type prev_ExecutorRun = NULL; static ExecutorFinish_hook_type prev_ExecutorFinish = NULL; static ExecutorEnd_hook_type prev_ExecutorEnd = NULL; -static bool explain_ExecutorStart(QueryDesc *queryDesc, int eflags); +static void explain_ExecutorStart(QueryDesc *queryDesc, int eflags); static void explain_ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count); @@ -261,11 +261,9 @@ _PG_init(void) /* * ExecutorStart hook: start up logging if needed */ -static bool +static void explain_ExecutorStart(QueryDesc *queryDesc, int eflags) { - bool plan_valid; - /* * At the beginning of each top-level statement, decide whether we'll * sample this statement. If nested-statement explaining is enabled, @@ -301,13 +299,9 @@ explain_ExecutorStart(QueryDesc *queryDesc, int eflags) } if (prev_ExecutorStart) - plan_valid = prev_ExecutorStart(queryDesc, eflags); + prev_ExecutorStart(queryDesc, eflags); else - plan_valid = standard_ExecutorStart(queryDesc, eflags); - - /* The plan may have become invalid during standard_ExecutorStart() */ - if (!plan_valid) - return false; + standard_ExecutorStart(queryDesc, eflags); if (auto_explain_enabled()) { @@ -325,8 +319,6 @@ explain_ExecutorStart(QueryDesc *queryDesc, int eflags) MemoryContextSwitchTo(oldcxt); } } - - return true; } /* diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c index 9778407cba3..d8fdf42df79 100644 --- a/contrib/pg_stat_statements/pg_stat_statements.c +++ b/contrib/pg_stat_statements/pg_stat_statements.c @@ -335,7 +335,7 @@ static PlannedStmt *pgss_planner(Query *parse, const char *query_string, int cursorOptions, ParamListInfo boundParams); -static bool pgss_ExecutorStart(QueryDesc *queryDesc, int eflags); +static void pgss_ExecutorStart(QueryDesc *queryDesc, int eflags); static void pgss_ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count); @@ -989,19 +989,13 @@ pgss_planner(Query *parse, /* * ExecutorStart hook: start up tracking if needed */ -static bool +static void pgss_ExecutorStart(QueryDesc *queryDesc, int eflags) { - bool plan_valid; - if (prev_ExecutorStart) - plan_valid = prev_ExecutorStart(queryDesc, eflags); + prev_ExecutorStart(queryDesc, eflags); else - plan_valid = standard_ExecutorStart(queryDesc, eflags); - - /* The plan may have become invalid during standard_ExecutorStart() */ - if (!plan_valid) - return false; + standard_ExecutorStart(queryDesc, eflags); /* * If query has queryId zero, don't track it. This prevents double @@ -1024,8 +1018,6 @@ pgss_ExecutorStart(QueryDesc *queryDesc, int eflags) MemoryContextSwitchTo(oldcxt); } } - - return true; } /* diff --git a/doc/src/sgml/release-18.sgml b/doc/src/sgml/release-18.sgml index cdf47ac6d2a..143a8b87647 100644 --- a/doc/src/sgml/release-18.sgml +++ b/doc/src/sgml/release-18.sgml @@ -588,27 +588,6 @@ Improve the locking performance of queries that access many relations (Tomas Von - - - - -Avoid the locking of pruned partitions during execution (Amit Langote) -§ -§ -§ -§ - - -