Ryan Kelly <rpkelly22@gmail.com> writes:
> Sorry about that. Couldn't seem to get a simple reproduction. I have now
> attached a straightforward one. I am not sure why so many columns are
> required to cause the error, so it'd be nice to understand that too.
Got it, thanks for the test case.
(For the archives' sake: this example doesn't fail for me with default
planner settings. I got it to fail after adding
set enable_seqscan to 0;
set enable_bitmapscan to 0;
Probably some less heavy-handed manipulation of the planner's cost
constants would do the trick too, but I didn't try.)
The problem occurs when the planner decides that an index-only scan
on the idx_claims_first_name_upper_trgm index is the thing to do,
because it generates an invalid plan in that case. Said plan used to
accidentally work before we tightened up what setrefs.c would allow,
which is how come you didn't see the failure before. The normal
case is probably to do an IOS on the claims_pkey index instead,
which'll work fine --- it's the fact that the gist_trgm_ops opclass
doesn't support returning column values that creates the issue.
But with an empty or near-empty table, the two index plans have
exactly the same estimated cost, so it's luck of the draw which
one you get. (I think it'll be the index with highest OID that gets
chosen in such cases, but that's an implementation artifact not
something to rely on.) I think the apparent dependency on number
of columns is a red herring. Possibly, depending on what cost
settings you are using, that could affect the relative cost
estimates for seqscan vs. index scan.
Anyway, the attached seems to be enough to fix it in HEAD.
It should apply more or less easily to v11 as well.
regards, tom lane
diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c
index cd6d72c763..fa069a217c 100644
--- a/src/backend/optimizer/plan/createplan.c
+++ b/src/backend/optimizer/plan/createplan.c
@@ -914,6 +914,22 @@ use_physical_tlist(PlannerInfo *root, Path *path, int flags)
return false;
}
+ /*
+ * For an index-only scan, the "physical tlist" is the index's indextlist.
+ * We can only return that without a projection if all the index's columns
+ * are returnable.
+ */
+ if (path->pathtype == T_IndexOnlyScan)
+ {
+ IndexOptInfo *indexinfo = ((IndexPath *) path)->indexinfo;
+
+ for (i = 0; i < indexinfo->ncolumns; i++)
+ {
+ if (!indexinfo->canreturn[i])
+ return false;
+ }
+ }
+
/*
* Also, can't do it if CP_LABEL_TLIST is specified and path is requested
* to emit any sort/group columns that are not simple Vars. (If they are