mirror of
				https://github.com/postgres/postgres.git
				synced 2025-10-29 22:49:41 +03:00 
			
		
		
		
	Fix unnecessary use of moving-aggregate mode with non-moving frame.
When a plain aggregate is used as a window function, and the window
frame start is specified as UNBOUNDED PRECEDING, the frame's head
cannot move so we do not need to use moving-aggregate mode.  The check
for that was put into initialize_peragg(), failing to notice that
ExecInitWindowAgg() calls that function before it's filled in
winstate->frameOptions.  Since makeNode() would have zeroed the field,
this didn't provoke uninitialized-value complaints, nor would the
erroneous decision have resulted in more than a little inefficiency.
Still, it's wrong, so move the initialization of
winstate->frameOptions earlier to make it work properly.
While here, also fix a thinko in a comment.  Both errors crept in in
commit a9d9acbf2 which introduced the moving-aggregate mode.
Spotted by Vallimaharajan G.  Back-patch to all supported branches.
Discussion: https://postgr.es/m/18e7f2a5167.fe36253866818.977923893562469143@zohocorp.com
			
			
This commit is contained in:
		| @@ -2273,6 +2273,9 @@ ExecInitWindowAgg(WindowAgg *node, EState *estate, int eflags) | |||||||
| 	winstate->ss.ps.state = estate; | 	winstate->ss.ps.state = estate; | ||||||
| 	winstate->ss.ps.ExecProcNode = ExecWindowAgg; | 	winstate->ss.ps.ExecProcNode = ExecWindowAgg; | ||||||
|  |  | ||||||
|  | 	/* copy frame options to state node for easy access */ | ||||||
|  | 	winstate->frameOptions = frameOptions; | ||||||
|  |  | ||||||
| 	/* | 	/* | ||||||
| 	 * Create expression contexts.  We need two, one for per-input-tuple | 	 * Create expression contexts.  We need two, one for per-input-tuple | ||||||
| 	 * processing and one for per-output-tuple processing.  We cheat a little | 	 * processing and one for per-output-tuple processing.  We cheat a little | ||||||
| @@ -2500,9 +2503,6 @@ ExecInitWindowAgg(WindowAgg *node, EState *estate, int eflags) | |||||||
| 		winstate->agg_winobj = agg_winobj; | 		winstate->agg_winobj = agg_winobj; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	/* copy frame options to state node for easy access */ |  | ||||||
| 	winstate->frameOptions = frameOptions; |  | ||||||
|  |  | ||||||
| 	/* initialize frame bound offset expressions */ | 	/* initialize frame bound offset expressions */ | ||||||
| 	winstate->startOffset = ExecInitExpr((Expr *) node->startOffset, | 	winstate->startOffset = ExecInitExpr((Expr *) node->startOffset, | ||||||
| 										 (PlanState *) winstate); | 										 (PlanState *) winstate); | ||||||
| @@ -2653,7 +2653,7 @@ initialize_peragg(WindowAggState *winstate, WindowFunc *wfunc, | |||||||
|  |  | ||||||
| 	/* | 	/* | ||||||
| 	 * Figure out whether we want to use the moving-aggregate implementation, | 	 * Figure out whether we want to use the moving-aggregate implementation, | ||||||
| 	 * and collect the right set of fields from the pg_attribute entry. | 	 * and collect the right set of fields from the pg_aggregate entry. | ||||||
| 	 * | 	 * | ||||||
| 	 * It's possible that an aggregate would supply a safe moving-aggregate | 	 * It's possible that an aggregate would supply a safe moving-aggregate | ||||||
| 	 * implementation and an unsafe normal one, in which case our hand is | 	 * implementation and an unsafe normal one, in which case our hand is | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user