Fix confusion in SP-GiST between attribute type and leaf storage type.
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 4 Apr 2021 18:28:35 +0000 (14:28 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 4 Apr 2021 18:28:57 +0000 (14:28 -0400)
commitac9099fc1dd460bffaafec19272159dd7bc86f5b
tree2d0547a416c0c4e8aaaec2dbcb1bcf36ff587599
parentd9c5b9a9eeb9e3061ae139e0e564ce5358c94001
Fix confusion in SP-GiST between attribute type and leaf storage type.

According to the documentation, the attType passed to the opclass
config function (and also relied on by the core code) is the type
of the heap column or expression being indexed.  But what was
actually being passed was the type stored for the index column.
This made no difference for user-defined SP-GiST opclasses,
because we weren't allowing the STORAGE clause of CREATE OPCLASS
to be used, so the two types would be the same.  But it's silly
not to allow that, seeing that the built-in poly_ops opclass
has a different value for opckeytype than opcintype, and that if you
want to do lossy storage then the types must really be different.
(Thus, user-defined opclasses doing lossy storage had to lie about
what type is in the index.)  Hence, remove the restriction, and make
sure that we use the input column type not opckeytype where relevant.

For reasons of backwards compatibility with existing user-defined
opclasses, we can't quite insist that the specified leafType match
the STORAGE clause; instead just add an amvalidate() warning if
they don't match.

Also fix some bugs that would only manifest when trying to return
index entries when attType is different from attLeafType.  It's not
too surprising that these have not been reported, because the only
usual reason for such a difference is to store the leaf value
lossily, rendering index-only scans impossible.

Add a src/test/modules module to exercise cases where attType is
different from attLeafType and yet index-only scan is supported.

Discussion: https://postgr.es/m/3728741.1617381471@sss.pgh.pa.us
17 files changed:
doc/src/sgml/ref/create_opclass.sgml
doc/src/sgml/spgist.sgml
src/backend/access/spgist/spgscan.c
src/backend/access/spgist/spgutils.c
src/backend/access/spgist/spgvalidate.c
src/include/access/spgist_private.h
src/test/modules/Makefile
src/test/modules/spgist_name_ops/.gitignore [new file with mode: 0644]
src/test/modules/spgist_name_ops/Makefile [new file with mode: 0644]
src/test/modules/spgist_name_ops/README [new file with mode: 0644]
src/test/modules/spgist_name_ops/expected/spgist_name_ops.out [new file with mode: 0644]
src/test/modules/spgist_name_ops/spgist_name_ops--1.0.sql [new file with mode: 0644]
src/test/modules/spgist_name_ops/spgist_name_ops.c [new file with mode: 0644]
src/test/modules/spgist_name_ops/spgist_name_ops.control [new file with mode: 0644]
src/test/modules/spgist_name_ops/sql/spgist_name_ops.sql [new file with mode: 0644]
src/test/regress/expected/rangetypes.out
src/test/regress/sql/rangetypes.sql