aboutsummaryrefslogtreecommitdiff
path: root/src/backend/access/spgist/spgvalidate.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2021-04-04 14:28:35 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2021-04-04 14:28:57 -0400
commitac9099fc1dd460bffaafec19272159dd7bc86f5b (patch)
tree2d0547a416c0c4e8aaaec2dbcb1bcf36ff587599 /src/backend/access/spgist/spgvalidate.c
parentd9c5b9a9eeb9e3061ae139e0e564ce5358c94001 (diff)
downloadpostgresql-ac9099fc1dd460bffaafec19272159dd7bc86f5b.tar.gz
postgresql-ac9099fc1dd460bffaafec19272159dd7bc86f5b.zip
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
Diffstat (limited to 'src/backend/access/spgist/spgvalidate.c')
-rw-r--r--src/backend/access/spgist/spgvalidate.c27
1 files changed, 24 insertions, 3 deletions
diff --git a/src/backend/access/spgist/spgvalidate.c b/src/backend/access/spgist/spgvalidate.c
index 8bc3889a4de..472a28b8080 100644
--- a/src/backend/access/spgist/spgvalidate.c
+++ b/src/backend/access/spgist/spgvalidate.c
@@ -43,6 +43,7 @@ spgvalidate(Oid opclassoid)
Form_pg_opclass classform;
Oid opfamilyoid;
Oid opcintype;
+ Oid opckeytype;
char *opclassname;
HeapTuple familytup;
Form_pg_opfamily familyform;
@@ -57,6 +58,7 @@ spgvalidate(Oid opclassoid)
spgConfigOut configOut;
Oid configOutLefttype = InvalidOid;
Oid configOutRighttype = InvalidOid;
+ Oid configOutLeafType = InvalidOid;
/* Fetch opclass information */
classtup = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclassoid));
@@ -66,6 +68,7 @@ spgvalidate(Oid opclassoid)
opfamilyoid = classform->opcfamily;
opcintype = classform->opcintype;
+ opckeytype = classform->opckeytype;
opclassname = NameStr(classform->opcname);
/* Fetch opfamily information */
@@ -118,13 +121,31 @@ spgvalidate(Oid opclassoid)
configOutLefttype = procform->amproclefttype;
configOutRighttype = procform->amprocrighttype;
+ /* Default leaf type is opckeytype or input type */
+ if (OidIsValid(opckeytype))
+ configOutLeafType = opckeytype;
+ else
+ configOutLeafType = procform->amproclefttype;
+
+ /* If some other leaf datum type is specified, warn */
+ if (OidIsValid(configOut.leafType) &&
+ configOutLeafType != configOut.leafType)
+ {
+ ereport(INFO,
+ (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
+ errmsg("SP-GiST leaf data type %s does not match declared type %s",
+ format_type_be(configOut.leafType),
+ format_type_be(configOutLeafType))));
+ result = false;
+ configOutLeafType = configOut.leafType;
+ }
+
/*
* When leaf and attribute types are the same, compress
* function is not required and we set corresponding bit in
* functionset for later group consistency check.
*/
- if (!OidIsValid(configOut.leafType) ||
- configOut.leafType == configIn.attType)
+ if (configOutLeafType == configIn.attType)
{
foreach(lc, grouplist)
{
@@ -156,7 +177,7 @@ spgvalidate(Oid opclassoid)
ok = false;
else
ok = check_amproc_signature(procform->amproc,
- configOut.leafType, true,
+ configOutLeafType, true,
1, 1, procform->amproclefttype);
break;
case SPGIST_OPTIONS_PROC: