Skip to content

Commit ac55abd

Browse files
committed
Disable WindowAgg inverse transitions when subplans are present
When an aggregate function is used as a WindowFunc and a tuple transitions out of the window frame, we ordinarily try to make use of the aggregate function's inverse transition function to "unaggregate" the exiting tuple. This optimization is disabled for various cases, including when the aggregate contains a volatile function. In such a case we'd be unable to ensure that the transition value was calculated to the same value during transitions and inverse transitions. Unfortunately, we did this check by calling contain_volatile_functions() which does not recursively search SubPlans for volatile functions. If the aggregate function's arguments or its FILTER clause contained a subplan with volatile functions then we'd fail to notice this. Here we fix this by just disabling the optimization when the WindowFunc contains any subplans. Volatile functions are not the only reason that a subplan may have nonrepeatable results. Bug: #17777 Reported-by: Anban Company Discussion: https://postgr.es/m/17777-860b739b6efde977%40postgresql.org Reviewed-by: Tom Lane Backpatch-through: 11
1 parent 11f1f9f commit ac55abd

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/backend/executor/nodeWindowAgg.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "executor/nodeWindowAgg.h"
4242
#include "miscadmin.h"
4343
#include "nodes/nodeFuncs.h"
44+
#include "optimizer/clauses.h"
4445
#include "optimizer/optimizer.h"
4546
#include "parser/parse_agg.h"
4647
#include "parser/parse_coerce.h"
@@ -2661,16 +2662,24 @@ initialize_peragg(WindowAggState *winstate, WindowFunc *wfunc,
26612662
* aggregate's arguments (and FILTER clause if any) contain any calls to
26622663
* volatile functions. Otherwise, the difference between restarting and
26632664
* not restarting the aggregation would be user-visible.
2665+
*
2666+
* We also don't risk using moving aggregates when there are subplans in
2667+
* the arguments or FILTER clause. This is partly because
2668+
* contain_volatile_functions() doesn't look inside subplans; but there
2669+
* are other reasons why a subplan's output might be volatile. For
2670+
* example, syncscan mode can render the results nonrepeatable.
26642671
*/
26652672
if (!OidIsValid(aggform->aggminvtransfn))
26662673
use_ma_code = false; /* sine qua non */
26672674
else if (aggform->aggmfinalmodify == AGGMODIFY_READ_ONLY &&
2668-
aggform->aggfinalmodify != AGGMODIFY_READ_ONLY)
2675+
aggform->aggfinalmodify != AGGMODIFY_READ_ONLY)
26692676
use_ma_code = true; /* decision forced by safety */
26702677
else if (winstate->frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING)
26712678
use_ma_code = false; /* non-moving frame head */
26722679
else if (contain_volatile_functions((Node *) wfunc))
26732680
use_ma_code = false; /* avoid possible behavioral change */
2681+
else if (contain_subplans((Node *) wfunc))
2682+
use_ma_code = false; /* subplans might contain volatile functions */
26742683
else
26752684
use_ma_code = true; /* yes, let's use it */
26762685
if (use_ma_code)

0 commit comments

Comments
 (0)