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

Lines changed: 9 additions & 1 deletion
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

Lines changed: 9 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy