/* *CachedPlanSource(whichmightbetterhavebeencalledCachedQuery) *representsaSQLquerythatweexpecttousemultipletimes.Itstoresthe *querysourcetext,thesourceparsetree,andtheanalyzed-and-rewritten *querytree,aswellasadjunctdata.Cacheinvalidationcanhappenasa *resultofDDLaffectingobjectsusedbythequery.Inthatcasewediscard *theanalyzed-and-rewrittenquerytree,andrebuilditwhennextneeded. * *Therearetwowaysinwhichthesourcequerycanberepresented:either *asarawparsetree,orasananalyzed-but-not-rewrittenparsetree. *Inthelattercaseweexpectthatcacheinvalidationneednotaffect *theparse-analysisresults,onlytherewritingandplanningsteps. *Onlyoneofraw_parse_treeandanalyzed_parse_treecanbenon-NULL. *(IfbothareNULL,theCachedPlanSourcerepresentsanemptyquery.) *Notethatquery_stringistypicallyjustanemptystringwhenthe *sourcequeryisananalyzedparsetree;also,param_types,num_params, *parserSetup,andparserSetupArgwillnotbeused. * *Anactualexecutionplan,representedbyCachedPlan,isderivedfromthe *CachedPlanSourcewhenweneedtoexecutethequery.Theplancouldbe *eithergeneric(usablewithanysetofplanparameters)orcustom(fora *specificsetofparameters).plancache.ccontainsthelogicthatdecides *whichwaytodoitforanyparticularexecution.Ifweareusingageneric *cachedplanthenitismeanttobere-usedacrossmultipleexecutions,so *callersmustalwaystreatCachedPlansasread-only. * *Oncesuccessfullybuiltand"saved",CachedPlanSourcestypicallylive *forthelifeofthebackend,althoughtheycanbedroppedexplicitly. *CachedPlansarereference-countedandgoawayautomaticallywhenthelast *referenceisdropped.ACachedPlancanoutlivetheCachedPlanSourceit *wascreatedfrom. * *An"unsaved"CachedPlanSourcecanbeusedforgeneratingplans,butit *livesintransientstorageandwillnotbeupdatedinresponsetosinval *events. * *CachedPlansmadefromsavedCachedPlanSourcesarelikewiseinpermanent *storage,sotoavoidmemoryleaks,thereference-countedreferencestothem *mustbeheldinpermanentdatastructuresorResourceOwners.CachedPlans *madefromunsavedCachedPlanSourcesareinchildrenofthecaller's *memorycontext,soreferencestothemshouldnotbelonger-livedthan *thatcontext.(Referencecountingissomewhatproformainthatcase, *thoughitmaybeusefuliftheCachedPlancanbediscardedearly.) * *ACachedPlanSourcehastwoassociatedmemorycontexts:onethatholdsthe *structitself,thequerysourcetextandthesourceparsetree,andanother *contextthatholdstherewrittenquerytreeandassociateddata.This *allowsthequerytreetobediscardedeasilywhenitisinvalidated. * *SomecallerswishtousetheCachedPlanAPIevenwithone-shotqueries *thathavenoreasontobesavedatall.Wethereforesupporta"oneshot" *variantthatdoesnodatacopyingorinvalidationchecking.Inthiscase *therearenoseparatememorycontexts:theCachedPlanSourcestructand *allsubsidiarydataliveinthecaller'sCurrentMemoryContext,andthere *isnowaytofreememoryshortofclearingthatentirecontext.Aoneshot *planisalwaystreatedasunsaved.
*/ typedefstruct CachedPlanSource
{ int magic; /* should equal CACHEDPLANSOURCE_MAGIC */ struct RawStmt *raw_parse_tree; /* output of raw_parser(), or NULL */ struct Query *analyzed_parse_tree; /* analyzed parse tree, or NULL */ constchar *query_string; /* source text of query */
CommandTag commandTag; /* command tag for query */
Oid *param_types; /* array of parameter type OIDs, or NULL */ int num_params; /* length of param_types array */
ParserSetupHook parserSetup; /* alternative parameter spec method */ void *parserSetupArg;
PostRewriteHook postRewrite; /* see SetPostRewriteHook */ void *postRewriteArg; int cursor_options; /* cursor options used for planning */ bool fixed_result; /* disallow change in result tupdesc? */
TupleDesc resultDesc; /* result type; NULL = doesn't return tuples */
MemoryContext context; /* memory context holding all above */ /* These fields describe the current analyzed-and-rewritten query tree: */
List *query_list; /* list of Query nodes, or NIL if not valid */
List *relationOids; /* OIDs of relations the queries depend on */
List *invalItems; /* other dependencies, as PlanInvalItems */ struct SearchPathMatcher *search_path; /* search_path used for parsing
* and planning */
MemoryContext query_context; /* context holding the above, or NULL */
Oid rewriteRoleId; /* Role ID we did rewriting for */ bool rewriteRowSecurity; /* row_security used during rewrite */ bool dependsOnRLS; /* is rewritten query specific to the above? */ /* If we have a generic plan, this is a reference-counted link to it: */ struct CachedPlan *gplan; /* generic plan, or NULL if not valid */ /* Some state flags: */ bool is_oneshot; /* is it a "oneshot" plan? */ bool is_complete; /* has CompleteCachedPlan been done? */ bool is_saved; /* has CachedPlanSource been "saved"? */ bool is_valid; /* is the query_list currently valid? */ int generation; /* increments each time we create a plan */ /* If CachedPlanSource has been saved, it is a member of a global list */
dlist_node node; /* list link, if is_saved */ /* State kept to help decide whether to use custom or generic plans: */ double generic_cost; /* cost of generic plan, or -1 if not known */ double total_custom_cost; /* total cost of custom plans so far */
int64 num_custom_plans; /* # of custom plans included in total */
int64 num_generic_plans; /* # of generic plans */
} CachedPlanSource;
/* *CachedPlanrepresentsanexecutionplanderivedfromaCachedPlanSource. *ThereferencecountincludesboththelinkfromtheparentCachedPlanSource *(ifany),andanyactiveplanexecutions,sotheplancanbediscarded *exactlywhenrefcountgoestozero.Boththestructitselfandthe *subsidiarydataliveinthecontextdenotedbythecontextfield. *Thismakesiteasytofreeano-longer-neededcachedplan.(However, *ifis_oneshotistrue,thecontextdoesnotbelongsolelytotheCachedPlan *sonofreeingispossible.)
*/ typedefstruct CachedPlan
{ int magic; /* should equal CACHEDPLAN_MAGIC */
List *stmt_list; /* list of PlannedStmts */ bool is_oneshot; /* is it a "oneshot" plan? */ bool is_saved; /* is CachedPlan in a long-lived context? */ bool is_valid; /* is the stmt_list currently valid? */
Oid planRoleId; /* Role ID the plan was created for */ bool dependsOnRole; /* is plan specific to that role? */
TransactionId saved_xmin; /* if valid, replan when TransactionXmin
* changes from this value */ int generation; /* parent's generation number for this plan */ int refcount; /* count of live references to this struct */
MemoryContext context; /* context containing this CachedPlan */
} CachedPlan;
/* *CachedExpressionisalow-overheadmechanismforcachingtheplannedform *ofstandalonescalarexpressions.Whilesuchexpressionsarenotusually *subjecttocacheinvalidationevents,thatcanhappen,forexamplebecause *ofreplacementofaSQLfunctionthatwasinlinedintotheexpression. *Theplancachetakescareofstoringtheexpressiontreeandmarkingit *invalidifacacheinvalidationoccurs,butthecallermustnoticethe *!is_validstatusanddiscardtheobsoleteexpressionwithoutreusingit. *Wedonotstoretheoriginalparsetree,onlytheplannedexpression; *thisisanoptimizationbasedontheassumptionthatweusuallywillnot *needtoreplanforthelifeofthesession.
*/ typedefstruct CachedExpression
{ int magic; /* should equal CACHEDEXPR_MAGIC */
Node *expr; /* planned form of expression */ bool is_valid; /* is the expression still valid? */ /* remaining fields should be treated as private to plancache.c: */
List *relationOids; /* OIDs of relations the expr depends on */
List *invalItems; /* other dependencies, as PlanInvalItems */
MemoryContext context; /* context containing this CachedExpression */
dlist_node node; /* link in global list of CachedExpressions */
} CachedExpression;
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.15Bemerkung:
(vorverarbeitet am 2026-08-08)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.