]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Prevent spurious "indexes on virtual generated columns are not supported".
authorRobert Haas <rhaas@postgresql.org>
Tue, 24 Mar 2026 10:11:15 +0000 (06:11 -0400)
committerRobert Haas <rhaas@postgresql.org>
Tue, 24 Mar 2026 10:28:32 +0000 (06:28 -0400)
Both of the checks in DefineIndex() that can produce this error
message have a guard against negative attribute numbers, but lack a
guard to ensure that attno is non-zero. As a result, we can index
off the beginning of the TupleDesc and read a garbage byte for
attgenerated. If that byte happens to be 'v', we'll incorrectly
produce the error mentioned above.

The first call site is easy to hit: any attempt to create an
expression index does so. The second one is not currently hit in
the regression tests, but can be hit by something like
CREATE INDEX ON some_table ((some_function(some_table))).

Found by study of a test_plan_advice failure on buildfarm member
skink, though this issue has nothing to do with test_plan_advice
and seems to have only been revealed by happenstance.

Backpatch-through: 18
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: http://postgr.es/m/CA+TgmoacixUZVvi00hOjk_d9B4iYKswWP1gNqQ8Vfray-AcOCA@mail.gmail.com

src/backend/commands/indexcmds.c

index 14679ff637370408641d49b1d809443a9b93fdb9..fd083e5ed2554bba4f9f99e4937f90c0c6278c90 100644 (file)
@@ -1116,7 +1116,8 @@ DefineIndex(Oid tableId,
                                         errmsg("index creation on system columns is not supported")));
 
 
-               if (TupleDescAttr(RelationGetDescr(rel), attno - 1)->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
+               if (attno > 0 &&
+                       TupleDescAttr(RelationGetDescr(rel), attno - 1)->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
                        ereport(ERROR,
                                        errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                        stmt->primary ?
@@ -1157,7 +1158,8 @@ DefineIndex(Oid tableId,
                {
                        AttrNumber      attno = j + FirstLowInvalidHeapAttributeNumber;
 
-                       if (TupleDescAttr(RelationGetDescr(rel), attno - 1)->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
+                       if (attno > 0 &&
+                               TupleDescAttr(RelationGetDescr(rel), attno - 1)->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
                                ereport(ERROR,
                                                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                                 stmt->isconstraint ?