Skip to content

Commit e5691cc

Browse files
committed
Don't use_physical_tlist for an IOS with non-returnable columns.
createplan.c tries to save a runtime projection step by specifying a scan plan node's output as being exactly the table's columns, or index's columns in the case of an index-only scan, if there is not a reason to do otherwise. This logic did not previously pay attention to whether an index's columns are returnable. That worked, sort of accidentally, until commit 9a3ddeb taught setrefs.c to reject plans that try to read a non-returnable column. I have no desire to loosen setrefs.c's new check, so instead adjust use_physical_tlist() to not try to optimize this way when there are non-returnable column(s). Per report from Ryan Kelly. Like the previous patch, back-patch to all supported branches. Discussion: https://postgr.es/m/CAHUie24ddN+pDNw7fkhNrjrwAX=fXXfGZZEHhRuofV_N_ftaSg@mail.gmail.com
1 parent 549ec20 commit e5691cc

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/backend/optimizer/plan/createplan.c

+16
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,22 @@ use_physical_tlist(PlannerInfo *root, Path *path, int flags)
914914
return false;
915915
}
916916

917+
/*
918+
* For an index-only scan, the "physical tlist" is the index's indextlist.
919+
* We can only return that without a projection if all the index's columns
920+
* are returnable.
921+
*/
922+
if (path->pathtype == T_IndexOnlyScan)
923+
{
924+
IndexOptInfo *indexinfo = ((IndexPath *) path)->indexinfo;
925+
926+
for (i = 0; i < indexinfo->ncolumns; i++)
927+
{
928+
if (!indexinfo->canreturn[i])
929+
return false;
930+
}
931+
}
932+
917933
/*
918934
* Also, can't do it if CP_LABEL_TLIST is specified and path is requested
919935
* to emit any sort/group columns that are not simple Vars. (If they are

src/test/regress/expected/gist.out

+16
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,22 @@ select p from gist_tbl where circle(p,1) @> circle(point(0,0),0.95);
357357
(0,0)
358358
(1 row)
359359

360+
-- Also check that use_physical_tlist doesn't trigger in such cases.
361+
explain (verbose, costs off)
362+
select count(*) from gist_tbl;
363+
QUERY PLAN
364+
---------------------------------------------------------------------
365+
Aggregate
366+
Output: count(*)
367+
-> Index Only Scan using gist_tbl_multi_index on public.gist_tbl
368+
(3 rows)
369+
370+
select count(*) from gist_tbl;
371+
count
372+
-------
373+
10001
374+
(1 row)
375+
360376
-- This case isn't supported, but it should at least EXPLAIN correctly.
361377
explain (verbose, costs off)
362378
select p from gist_tbl order by circle(p,1) <-> point(0,0) limit 1;

src/test/regress/sql/gist.sql

+5
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ explain (verbose, costs off)
159159
select p from gist_tbl where circle(p,1) @> circle(point(0,0),0.95);
160160
select p from gist_tbl where circle(p,1) @> circle(point(0,0),0.95);
161161

162+
-- Also check that use_physical_tlist doesn't trigger in such cases.
163+
explain (verbose, costs off)
164+
select count(*) from gist_tbl;
165+
select count(*) from gist_tbl;
166+
162167
-- This case isn't supported, but it should at least EXPLAIN correctly.
163168
explain (verbose, costs off)
164169
select p from gist_tbl order by circle(p,1) <-> point(0,0) limit 1;

0 commit comments

Comments
 (0)