]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Add planner_setup_hook and planner_shutdown_hook.
authorRobert Haas <rhaas@postgresql.org>
Wed, 8 Oct 2025 13:05:38 +0000 (09:05 -0400)
committerRobert Haas <rhaas@postgresql.org>
Wed, 8 Oct 2025 13:05:38 +0000 (09:05 -0400)
These hooks allow plugins to get control at the earliest point at
which the PlannerGlobal object is fully initialized, and then just
before it gets destroyed. This is useful in combination with the
extendable plan state facilities (see extendplan.h) and perhaps for
other purposes as well.

Reviewed-by: Andrei Lepikhov <lepihov@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: http://postgr.es/m/CA+TgmoYWKHU2hKr62Toyzh-kTDEnMDeLw7gkOOnjL-TnOUq0kQ@mail.gmail.com

src/backend/optimizer/plan/planner.c
src/include/optimizer/planner.h

index c250433a2ef43bbcd95579e4cf1627b9a4463514..e8ea78c0c977258f2c5f2619f8743480ddad64bd 100644 (file)
@@ -73,6 +73,12 @@ bool         enable_distinct_reordering = true;
 /* Hook for plugins to get control in planner() */
 planner_hook_type planner_hook = NULL;
 
+/* Hook for plugins to get control after PlannerGlobal is initialized */
+planner_setup_hook_type planner_setup_hook = NULL;
+
+/* Hook for plugins to get control before PlannerGlobal is discarded */
+planner_shutdown_hook_type planner_shutdown_hook = NULL;
+
 /* Hook for plugins to get control when grouping_planner() plans upper rels */
 create_upper_paths_hook_type create_upper_paths_hook = NULL;
 
@@ -456,6 +462,10 @@ standard_planner(Query *parse, const char *query_string, int cursorOptions,
                tuple_fraction = 0.0;
        }
 
+       /* Allow plugins to take control after we've initialized "glob" */
+       if (planner_setup_hook)
+               (*planner_setup_hook) (glob, parse, query_string, &tuple_fraction, es);
+
        /* primary planning entry point (may recurse for subqueries) */
        root = subquery_planner(glob, parse, NULL, NULL, false, tuple_fraction,
                                                        NULL);
@@ -635,6 +645,10 @@ standard_planner(Query *parse, const char *query_string, int cursorOptions,
                        result->jitFlags |= PGJIT_DEFORM;
        }
 
+       /* Allow plugins to take control before we discard "glob" */
+       if (planner_shutdown_hook)
+               (*planner_shutdown_hook) (glob, parse, query_string, result);
+
        if (glob->partition_directory != NULL)
                DestroyPartitionDirectory(glob->partition_directory);
 
index c7ab466f5f1f4bf6564e78a033709bfdf279baca..55d9b7940aa9ee81f906af432748fc523dad01c2 100644 (file)
@@ -32,6 +32,19 @@ typedef PlannedStmt *(*planner_hook_type) (Query *parse,
                                                                                   ExplainState *es);
 extern PGDLLIMPORT planner_hook_type planner_hook;
 
+/* Hook for plugins to get control after PlannerGlobal is initialized */
+typedef void (*planner_setup_hook_type) (PlannerGlobal *glob, Query *parse,
+                                                                                const char *query_string,
+                                                                                double *tuple_fraction,
+                                                                                ExplainState *es);
+extern PGDLLIMPORT planner_setup_hook_type planner_setup_hook;
+
+/* Hook for plugins to get control before PlannerGlobal is discarded */
+typedef void (*planner_shutdown_hook_type) (PlannerGlobal *glob, Query *parse,
+                                                                                       const char *query_string,
+                                                                                       PlannedStmt *pstmt);
+extern PGDLLIMPORT planner_shutdown_hook_type planner_shutdown_hook;
+
 /* Hook for plugins to get control when grouping_planner() plans upper rels */
 typedef void (*create_upper_paths_hook_type) (PlannerInfo *root,
                                                                                          UpperRelationKind stage,