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

Commit 249126e

Browse files
committed
Use context with correct lifetime in hypothetical_dense_rank_final.
The query lifetime expression context created in hypothetical_dense_rank_final() was buggily allocated in the calling memory context. I (Andres) broke that in bf6c614. Reported-By: Rajkumar Raghuwanshi Author: Amit Langote Discussion: https://postgr.es/m/CAKcux6kmzWmur5HhA_aU6gYVFu0RLQdgJJ+aC9SLdcOvBSrpfA@mail.gmail.com Backpatch: 11-
1 parent 3a01f68 commit 249126e

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/backend/utils/adt/orderedsetaggs.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -1310,7 +1310,15 @@ hypothetical_dense_rank_final(PG_FUNCTION_ARGS)
13101310
osastate = (OSAPerGroupState *) PG_GETARG_POINTER(0);
13111311
econtext = osastate->qstate->econtext;
13121312
if (!econtext)
1313-
osastate->qstate->econtext = econtext = CreateStandaloneExprContext();
1313+
{
1314+
MemoryContext oldcontext;
1315+
1316+
/* Make sure to we create econtext under correct parent context. */
1317+
oldcontext = MemoryContextSwitchTo(osastate->qstate->qcontext);
1318+
osastate->qstate->econtext = CreateStandaloneExprContext();
1319+
econtext = osastate->qstate->econtext;
1320+
MemoryContextSwitchTo(oldcontext);
1321+
}
13141322

13151323
/* Adjust nargs to be the number of direct (or aggregated) args */
13161324
if (nargs % 2 != 0)

src/test/regress/expected/aggregates.out

+9
Original file line numberDiff line numberDiff line change
@@ -2092,3 +2092,12 @@ SELECT variance(unique1::int4), sum(unique1::int8) FROM tenk1;
20922092
(1 row)
20932093

20942094
ROLLBACK;
2095+
-- test coverage for dense_rank
2096+
SELECT dense_rank(x) WITHIN GROUP (ORDER BY x) FROM (VALUES (1),(1),(2),(2),(3),(3)) v(x) GROUP BY (x) ORDER BY 1;
2097+
dense_rank
2098+
------------
2099+
1
2100+
1
2101+
1
2102+
(3 rows)
2103+

src/test/regress/sql/aggregates.sql

+3
Original file line numberDiff line numberDiff line change
@@ -925,3 +925,6 @@ EXPLAIN (COSTS OFF)
925925
SELECT variance(unique1::int4), sum(unique1::int8) FROM tenk1;
926926

927927
ROLLBACK;
928+
929+
-- test coverage for dense_rank
930+
SELECT dense_rank(x) WITHIN GROUP (ORDER BY x) FROM (VALUES (1),(1),(2),(2),(3),(3)) v(x) GROUP BY (x) ORDER BY 1;

0 commit comments

Comments
 (0)