1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-26 01:22:12 +03:00

Cache if PathTarget and RestrictInfos contain volatile functions

Here we aim to reduce duplicate work done by contain_volatile_functions()
by caching whether PathTargets and RestrictInfos contain any volatile
functions the first time contain_volatile_functions() is called for them.
Any future calls for these nodes just use the cached value rather than
going to the trouble of recursively checking the sub-node all over again.
Thanks to Tom Lane for the idea.

Any locations in the code which make changes to a PathTarget or
RestrictInfo which could change the outcome of the volatility check must
change the cached value back to VOLATILITY_UNKNOWN again.
contain_volatile_functions() is the only code in charge of setting the
cache value to either VOLATILITY_VOLATILE or VOLATILITY_NOVOLATILE.

Some existing code does benefit from this additional caching, however,
this change is mainly aimed at an upcoming patch that must check for
volatility during the join search.  Repeated volatility checks in that
case can become very expensive when the join search contains more than a
few relations.

Author: David Rowley
Discussion: https://postgr.es/m/3795226.1614059027@sss.pgh.pa.us
This commit is contained in:
David Rowley
2021-03-29 14:47:05 +13:00
parent b64654d6c4
commit f58b230ed0
8 changed files with 134 additions and 20 deletions

View File

@ -1055,6 +1055,17 @@ typedef struct PathKey
bool pk_nulls_first; /* do NULLs come before normal values? */
} PathKey;
/*
* VolatileFunctionStatus -- allows nodes to cache their
* contain_volatile_functions properties. VOLATILITY_UNKNOWN means not yet
* determined.
*/
typedef enum VolatileFunctionStatus
{
VOLATILITY_UNKNOWN = 0,
VOLATILITY_VOLATILE,
VOLATILITY_NOVOLATILE
} VolatileFunctionStatus;
/*
* PathTarget
@ -1086,6 +1097,8 @@ typedef struct PathTarget
Index *sortgrouprefs; /* corresponding sort/group refnos, or 0 */
QualCost cost; /* cost of evaluating the expressions */
int width; /* estimated avg width of result tuples */
VolatileFunctionStatus has_volatile_expr; /* indicates if exprs contain
* any volatile functions. */
} PathTarget;
/* Convenience macro to get a sort/group refno from a PathTarget */
@ -2016,6 +2029,9 @@ typedef struct RestrictInfo
bool leakproof; /* true if known to contain no leaked Vars */
VolatileFunctionStatus has_volatile; /* to indicate if clause contains
* any volatile functions. */
Index security_level; /* see comment above */
/* The set of relids (varnos) actually referenced in the clause: */