Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f8bf567

Browse files
tglsfdcCommitfest Bot
authored and
Commitfest Bot
committed
Provide a post-rewrite callback hook in plancache.c.
SQL-language functions sometimes want to modify the targetlist of the query that returns their result. If they're to use the plan cache, it needs to be possible to do that over again when a replan occurs. Invent a callback hook to make that happen. I chose to provide a separate function SetPostRewriteHook to install such hooks. An alternative API could be to add two more arguments to CompleteCachedPlan. I didn't do so because I felt that few callers will want this, but there's a case that that way would be cleaner. Author: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/8216639.NyiUUSuA9g@aivenlaptop
1 parent c43855e commit f8bf567

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/backend/utils/cache/plancache.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ CreateCachedPlan(RawStmt *raw_parse_tree,
219219
plansource->num_params = 0;
220220
plansource->parserSetup = NULL;
221221
plansource->parserSetupArg = NULL;
222+
plansource->postRewrite = NULL;
223+
plansource->postRewriteArg = NULL;
222224
plansource->cursor_options = 0;
223225
plansource->fixed_result = false;
224226
plansource->resultDesc = NULL;
@@ -316,6 +318,8 @@ CreateOneShotCachedPlan(RawStmt *raw_parse_tree,
316318
plansource->num_params = 0;
317319
plansource->parserSetup = NULL;
318320
plansource->parserSetupArg = NULL;
321+
plansource->postRewrite = NULL;
322+
plansource->postRewriteArg = NULL;
319323
plansource->cursor_options = 0;
320324
plansource->fixed_result = false;
321325
plansource->resultDesc = NULL;
@@ -485,6 +489,29 @@ CompleteCachedPlan(CachedPlanSource *plansource,
485489
plansource->is_valid = true;
486490
}
487491

492+
/*
493+
* SetPostRewriteHook: set a hook to modify post-rewrite query trees
494+
*
495+
* Some callers have a need to modify the query trees between rewriting and
496+
* planning. In the initial call to CompleteCachedPlan, it's assumed such
497+
* work was already done on the querytree_list. However, if we're forced
498+
* to replan, it will need to be done over. The caller can set this hook
499+
* to provide code to make that happen.
500+
*
501+
* postRewriteArg is just passed verbatim to the hook. As with parserSetupArg,
502+
* it is caller's responsibility that the referenced data remains
503+
* valid for as long as the CachedPlanSource exists.
504+
*/
505+
void
506+
SetPostRewriteHook(CachedPlanSource *plansource,
507+
PostRewriteHook postRewrite,
508+
void *postRewriteArg)
509+
{
510+
Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
511+
plansource->postRewrite = postRewrite;
512+
plansource->postRewriteArg = postRewriteArg;
513+
}
514+
488515
/*
489516
* SaveCachedPlan: save a cached plan permanently
490517
*
@@ -813,6 +840,10 @@ RevalidateCachedQuery(CachedPlanSource *plansource,
813840
tlist = NIL;
814841
}
815842

843+
/* Apply post-rewrite callback if there is one */
844+
if (plansource->postRewrite != NULL)
845+
plansource->postRewrite(tlist, plansource->postRewriteArg);
846+
816847
/* Release snapshot if we got one */
817848
if (snapshot_set)
818849
PopActiveSnapshot();
@@ -1800,6 +1831,8 @@ CopyCachedPlan(CachedPlanSource *plansource)
18001831
newsource->num_params = plansource->num_params;
18011832
newsource->parserSetup = plansource->parserSetup;
18021833
newsource->parserSetupArg = plansource->parserSetupArg;
1834+
newsource->postRewrite = plansource->postRewrite;
1835+
newsource->postRewriteArg = plansource->postRewriteArg;
18031836
newsource->cursor_options = plansource->cursor_options;
18041837
newsource->fixed_result = plansource->fixed_result;
18051838
if (plansource->resultDesc)

src/include/utils/plancache.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ typedef enum
4040
/* GUC parameter */
4141
extern PGDLLIMPORT int plan_cache_mode;
4242

43+
/* Optional callback to editorialize on rewritten parse trees */
44+
typedef void (*PostRewriteHook) (List *querytree_list, void *arg);
45+
4346
#define CACHEDPLANSOURCE_MAGIC 195726186
4447
#define CACHEDPLAN_MAGIC 953717834
4548
#define CACHEDEXPR_MAGIC 838275847
@@ -112,6 +115,8 @@ typedef struct CachedPlanSource
112115
int num_params; /* length of param_types array */
113116
ParserSetupHook parserSetup; /* alternative parameter spec method */
114117
void *parserSetupArg;
118+
PostRewriteHook postRewrite; /* see SetPostRewriteHook */
119+
void *postRewriteArg;
115120
int cursor_options; /* cursor options used for planning */
116121
bool fixed_result; /* disallow change in result tupdesc? */
117122
TupleDesc resultDesc; /* result type; NULL = doesn't return tuples */
@@ -223,6 +228,9 @@ extern void CompleteCachedPlan(CachedPlanSource *plansource,
223228
void *parserSetupArg,
224229
int cursor_options,
225230
bool fixed_result);
231+
extern void SetPostRewriteHook(CachedPlanSource *plansource,
232+
PostRewriteHook postRewrite,
233+
void *postRewriteArg);
226234

227235
extern void SaveCachedPlan(CachedPlanSource *plansource);
228236
extern void DropCachedPlan(CachedPlanSource *plansource);

src/tools/pgindent/typedefs.list

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,6 +2266,7 @@ PortalHashEnt
22662266
PortalStatus
22672267
PortalStrategy
22682268
PostParseColumnRefHook
2269+
PostRewriteHook
22692270
PostgresPollingStatusType
22702271
PostingItem
22712272
PreParseColumnRefHook

0 commit comments

Comments
 (0)