Dissociate btequalimage() from interval_ops, ending its deduplication.
authorNoah Misch
Sat, 14 Oct 2023 23:33:51 +0000 (16:33 -0700)
committerNoah Misch
Sat, 14 Oct 2023 23:33:51 +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[email protected]

contrib/amcheck/verify_nbtree.c
src/include/catalog/catversion.h
src/include/catalog/pg_amproc.dat
src/include/catalog/pg_opfamily.dat
src/test/regress/expected/opr_sanity.out

index dbb83d80f81b8a8a32c29705201ba7d103d55b22..3e07a3e35f4327f6af084d01ca379188c7b86c07 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 "common/pg_prng.h"
 #include "lib/bloomfilter.h"
@@ -338,10 +339,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 473687419c95cc0aeab19230e58a77369c9a563e..5efc1445207017c2e4ec57414fadcb853087f13b 100644 (file)
@@ -57,6 +57,6 @@
  */
 
 /*                         yyyymmddN */
-#define CATALOG_VERSION_NO 202310131
+#define CATALOG_VERSION_NO 202310141
 
 #endif
index 5b950129de0aa8cb66b7b93be66179e3d309547a..4c70da41defd6b81225d0d7d54ec8cfd330238a6 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 91587b99d09729cc4a881fd2d1b64644e72dd32e..81a8525c5d0757e43c1afe5ea3b4aac7ac54110e 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 a1bdf2c0b5f81abdd8de071aa37373087f9101d3..7a6f36a6a9a8ff29ca2568e636de902281857e60 100644 (file)
@@ -2208,6 +2208,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
                     | multirange_ops   | multirange_ops   | anymultirange
                     | numeric_ops      | numeric_ops      | numeric
@@ -2216,7 +2217,7 @@ ORDER BY 1, 2, 3;
                     | record_ops       | record_ops       | record
                     | tsquery_ops      | tsquery_ops      | tsquery
                     | tsvector_ops     | tsvector_ops     | tsvector
-(15 rows)
+(16 rows)
 
 -- **************** pg_index ****************
 -- Look for illegal values in pg_index fields.