Dissociate btequalimage() from interval_ops, ending its deduplication.
authorNoah Misch <noah@leadboat.com>
Sat, 14 Oct 2023 23:33:51 +0000 (16:33 -0700)
committerNoah Misch <noah@leadboat.com>
Sat, 14 Oct 2023 23:33:54 +0000 (16:33 -0700)
Under interval_ops, some equal values are distinguishable.  One such
pair is '24:00:00' and '1 day'.  With that being so, btequalimage()
breaches the documented contract for the "equalimage" btree support
function.  This can cause incorrect results from index-only scans.
Users should REINDEX any btree indexes having interval-type columns.
After updating, pg_amcheck will report an error for almost all such
indexes.  This fix makes interval_ops simply omit the support function,
like numeric_ops does.  Back-pack to v13, where btequalimage() first
appeared.  In back branches, for the benefit of old catalog content,
btequalimage() code will return false for type "interval".  Going
forward, back-branch initdb will include the catalog change.

Reviewed by Peter Geoghegan.

Discussion: https://postgr.es/m/20231011013317.22.nmisch@google.com

contrib/amcheck/verify_nbtree.c
src/backend/utils/adt/datum.c
src/include/catalog/pg_amproc.dat
src/include/catalog/pg_opfamily.dat
src/test/regress/expected/opr_sanity.out

index 63d1f0df4dc71f6cc011cdec2d381583eb7616c7..b09dcc08077a8e1cd5d8bce1cd5153b0f5891c2d 100644 (file)
@@ -31,6 +31,7 @@
 #include "access/xact.h"
 #include "catalog/index.h"
 #include "catalog/pg_am.h"
+#include "catalog/pg_opfamily_d.h"
 #include "commands/tablecmds.h"
 #include "lib/bloomfilter.h"
 #include "miscadmin.h"
@@ -333,10 +334,20 @@ bt_index_check_internal(Oid indrelid, bool parentcheck, bool heapallindexed,
                     errmsg("index \"%s\" metapage has equalimage field set on unsupported nbtree version",
                            RelationGetRelationName(indrel))));
        if (allequalimage && !_bt_allequalimage(indrel, false))
+       {
+           bool        has_interval_ops = false;
+
+           for (int i = 0; i < IndexRelationGetNumberOfKeyAttributes(indrel); i++)
+               if (indrel->rd_opfamily[i] == INTERVAL_BTREE_FAM_OID)
+                   has_interval_ops = true;
            ereport(ERROR,
                    (errcode(ERRCODE_INDEX_CORRUPTED),
                     errmsg("index \"%s\" metapage incorrectly indicates that deduplication is safe",
-                           RelationGetRelationName(indrel))));
+                           RelationGetRelationName(indrel)),
+                    has_interval_ops
+                    ? errhint("This is known of \"interval\" indexes last built on a version predating 2023-11.")
+                    : 0));
+       }
 
        /* Check index, possibly against table it is an index on */
        bt_check_every_level(indrel, heaprel, heapkeyspace, parentcheck,
index 34cdde1bb91bb4767911499af124dba733787666..fcc3c38c20015699bad49cd65267782449c24b43 100644 (file)
@@ -43,6 +43,7 @@
 #include "postgres.h"
 
 #include "access/detoast.h"
+#include "catalog/pg_type_d.h"
 #include "fmgr.h"
 #include "utils/builtins.h"
 #include "utils/datum.h"
@@ -333,20 +334,17 @@ datum_image_eq(Datum value1, Datum value2, bool typByVal, int typLen)
  * datum_image_eq() in all cases can use this as their "equalimage" support
  * function.
  *
- * Currently, we unconditionally assume that any B-Tree operator class that
- * registers btequalimage as its support function 4 must be able to safely use
- * optimizations like deduplication (i.e. we return true unconditionally).  If
- * it ever proved necessary to rescind support for an operator class, we could
- * do that in a targeted fashion by doing something with the opcintype
- * argument.
+ * Earlier minor releases erroneously associated this function with
+ * interval_ops.  Detect that case to rescind deduplication support, without
+ * requiring initdb.
  *-------------------------------------------------------------------------
  */
 Datum
 btequalimage(PG_FUNCTION_ARGS)
 {
-   /* Oid      opcintype = PG_GETARG_OID(0); */
+   Oid         opcintype = PG_GETARG_OID(0);
 
-   PG_RETURN_BOOL(true);
+   PG_RETURN_BOOL(opcintype != INTERVALOID);
 }
 
 /*-------------------------------------------------------------------------
index 37b580883fcb90cb5807fa55332bde14fe086be4..6edd1f12c7954fef0884d9584ff2d1c34a8d4383 100644 (file)
 { amprocfamily => 'btree/interval_ops', amproclefttype => 'interval',
   amprocrighttype => 'interval', amprocnum => '3',
   amproc => 'in_range(interval,interval,interval,bool,bool)' },
-{ amprocfamily => 'btree/interval_ops', amproclefttype => 'interval',
-  amprocrighttype => 'interval', amprocnum => '4', amproc => 'btequalimage' },
 { amprocfamily => 'btree/macaddr_ops', amproclefttype => 'macaddr',
   amprocrighttype => 'macaddr', amprocnum => '1', amproc => 'macaddr_cmp' },
 { amprocfamily => 'btree/macaddr_ops', amproclefttype => 'macaddr',
index cf0fb325b3426e89413435a1ded196726ce81b38..59067bb8276a504bb0f58b6194ce08bb3356fb96 100644 (file)
@@ -50,7 +50,7 @@
   opfmethod => 'btree', opfname => 'integer_ops' },
 { oid => '1977',
   opfmethod => 'hash', opfname => 'integer_ops' },
-{ oid => '1982',
+{ oid => '1982', oid_symbol => 'INTERVAL_BTREE_FAM_OID',
   opfmethod => 'btree', opfname => 'interval_ops' },
 { oid => '1983',
   opfmethod => 'hash', opfname => 'interval_ops' },
index 27056d70d36dd3653490d80f38e35672cfc54dc6..2fdfc7ba8bd87d6360a055a446f285bccf22cb0e 100644 (file)
@@ -2192,6 +2192,7 @@ ORDER BY 1, 2, 3;
                     | array_ops        | array_ops        | anyarray
                     | float_ops        | float4_ops       | real
                     | float_ops        | float8_ops       | double precision
+                    | interval_ops     | interval_ops     | interval
                     | jsonb_ops        | jsonb_ops        | jsonb
                     | numeric_ops      | numeric_ops      | numeric
                     | range_ops        | range_ops        | anyrange
@@ -2199,7 +2200,7 @@ ORDER BY 1, 2, 3;
                     | record_ops       | record_ops       | record
                     | tsquery_ops      | tsquery_ops      | tsquery
                     | tsvector_ops     | tsvector_ops     | tsvector
-(14 rows)
+(15 rows)
 
 -- **************** pg_index ****************
 -- Look for illegal values in pg_index fields.