Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8a85969

Browse files
committed
Properly initialize SortSupport for ORDER BY rechecks in nodeIndexscan.c.
Fix still another bug in commit 35fcb1b: it failed to fully initialize the SortSupport states it introduced to allow the executor to re-check ORDER BY expressions containing distance operators. That led to a null pointer dereference if the sortsupport code tried to use ssup_cxt. The problem only manifests in narrow cases, explaining the lack of previous field reports. It requires a GiST-indexable distance operator that lacks SortSupport and is on a pass-by-ref data type, which among core+contrib seems to be only btree_gist's interval opclass; and it requires the scan to be done as an IndexScan not an IndexOnlyScan, which explains how btree_gist's regression test didn't catch it. Per bug #14134 from Jihyun Yu. Peter Geoghegan Report: <[email protected]>
1 parent de33af8 commit 8a85969

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

contrib/btree_gist/expected/interval.out

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,21 @@ SELECT a, a <-> '199 days 21:21:23' FROM intervaltmp ORDER BY a <-> '199 days 21
8989
@ 220 days 19 hours 5 mins 42 secs | @ 21 days -2 hours -15 mins -41 secs
9090
(3 rows)
9191

92+
SET enable_indexonlyscan=off;
93+
EXPLAIN (COSTS OFF)
94+
SELECT a, a <-> '199 days 21:21:23' FROM intervaltmp ORDER BY a <-> '199 days 21:21:23' LIMIT 3;
95+
QUERY PLAN
96+
---------------------------------------------------------------------------
97+
Limit
98+
-> Index Scan using intervalidx on intervaltmp
99+
Order By: (a <-> '@ 199 days 21 hours 21 mins 23 secs'::interval)
100+
(3 rows)
101+
102+
SELECT a, a <-> '199 days 21:21:23' FROM intervaltmp ORDER BY a <-> '199 days 21:21:23' LIMIT 3;
103+
a | ?column?
104+
-------------------------------------+--------------------------------------
105+
@ 199 days 21 hours 21 mins 23 secs | @ 0
106+
@ 183 days 6 hours 52 mins 48 secs | @ 16 days 14 hours 28 mins 35 secs
107+
@ 220 days 19 hours 5 mins 42 secs | @ 21 days -2 hours -15 mins -41 secs
108+
(3 rows)
109+

contrib/btree_gist/sql/interval.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,9 @@ SELECT count(*) FROM intervaltmp WHERE a > '199 days 21:21:23'::interval;
3535
EXPLAIN (COSTS OFF)
3636
SELECT a, a <-> '199 days 21:21:23' FROM intervaltmp ORDER BY a <-> '199 days 21:21:23' LIMIT 3;
3737
SELECT a, a <-> '199 days 21:21:23' FROM intervaltmp ORDER BY a <-> '199 days 21:21:23' LIMIT 3;
38+
39+
SET enable_indexonlyscan=off;
40+
41+
EXPLAIN (COSTS OFF)
42+
SELECT a, a <-> '199 days 21:21:23' FROM intervaltmp ORDER BY a <-> '199 days 21:21:23' LIMIT 3;
43+
SELECT a, a <-> '199 days 21:21:23' FROM intervaltmp ORDER BY a <-> '199 days 21:21:23' LIMIT 3;

src/backend/executor/nodeIndexscan.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -967,9 +967,20 @@ ExecInitIndexScan(IndexScan *node, EState *estate, int eflags)
967967
Oid orderbyop = lfirst_oid(lco);
968968
Node *orderbyexpr = (Node *) lfirst(lcx);
969969
Oid orderbyType = exprType(orderbyexpr);
970+
Oid orderbyColl = exprCollation(orderbyexpr);
971+
SortSupport orderbysort = &indexstate->iss_SortSupport[i];
972+
973+
/* Initialize sort support */
974+
orderbysort->ssup_cxt = CurrentMemoryContext;
975+
orderbysort->ssup_collation = orderbyColl;
976+
/* See cmp_orderbyvals() comments on NULLS LAST */
977+
orderbysort->ssup_nulls_first = false;
978+
/* ssup_attno is unused here and elsewhere */
979+
orderbysort->ssup_attno = 0;
980+
/* No abbreviation */
981+
orderbysort->abbreviate = false;
982+
PrepareSortSupportFromOrderingOp(orderbyop, orderbysort);
970983

971-
PrepareSortSupportFromOrderingOp(orderbyop,
972-
&indexstate->iss_SortSupport[i]);
973984
get_typlenbyval(orderbyType,
974985
&indexstate->iss_OrderByTypLens[i],
975986
&indexstate->iss_OrderByTypByVals[i]);

0 commit comments

Comments
 (0)