Change gist stratnum function to use CompareType
authorPeter Eisentraut <peter@eisentraut.org>
Wed, 15 Jan 2025 10:28:39 +0000 (11:28 +0100)
committerPeter Eisentraut <peter@eisentraut.org>
Wed, 15 Jan 2025 10:34:04 +0000 (11:34 +0100)
This changes commit 7406ab623fe in that the gist strategy number
mapping support function is changed to use the CompareType enum as
input, instead of the "well-known" RT*StrategyNumber strategy numbers.

This is a bit cleaner, since you are not dealing with two sets of
strategy numbers.  Also, this will enable us to subsume this system
into a more general system of using CompareType to define operator
semantics across index methods.

Discussion: https://www.postgresql.org/message-id/flat/E72EAA49-354D-4C2E-8EB9-255197F55330@enterprisedb.com

19 files changed:
contrib/btree_gist/btree_gist--1.7--1.8.sql
contrib/btree_gist/btree_gist.c
contrib/btree_gist/expected/stratnum.out
contrib/btree_gist/sql/stratnum.sql
doc/src/sgml/gist.sgml
doc/src/sgml/xindex.sgml
src/backend/access/gist/gistutil.c
src/backend/access/gist/gistvalidate.c
src/backend/catalog/pg_constraint.c
src/backend/commands/indexcmds.c
src/backend/commands/tablecmds.c
src/backend/executor/execReplication.c
src/include/access/gist.h
src/include/catalog/pg_amproc.dat
src/include/catalog/pg_proc.dat
src/include/commands/defrem.h
src/include/nodes/primnodes.h
src/test/regress/expected/misc_functions.out
src/test/regress/sql/misc_functions.sql

index 307bfe574b0b66d83ee7e0afe82a4ab6481b17f4..c702426deabb5eca0f3e2c7cd2d8237482d5a6a1 100644 (file)
@@ -3,85 +3,85 @@
 -- complain if script is sourced in psql, rather than via CREATE EXTENSION
 \echo Use "ALTER EXTENSION btree_gist UPDATE TO '1.8'" to load this file. \quit
 
-CREATE FUNCTION gist_stratnum_btree(smallint)
+CREATE FUNCTION gist_stratnum_btree(int)
 RETURNS smallint
 AS 'MODULE_PATHNAME'
 LANGUAGE C IMMUTABLE PARALLEL SAFE STRICT;
 
 ALTER OPERATOR FAMILY gist_oid_ops USING gist ADD
-       FUNCTION 12 (oid, oid) gist_stratnum_btree (int2) ;
+       FUNCTION 12 (oid, oid) gist_stratnum_btree (int) ;
 
 ALTER OPERATOR FAMILY gist_int2_ops USING gist ADD
-       FUNCTION 12 (int2, int2) gist_stratnum_btree (int2) ;
+       FUNCTION 12 (int2, int2) gist_stratnum_btree (int) ;
 
 ALTER OPERATOR FAMILY gist_int4_ops USING gist ADD
-       FUNCTION 12 (int4, int4) gist_stratnum_btree (int2) ;
+       FUNCTION 12 (int4, int4) gist_stratnum_btree (int) ;
 
 ALTER OPERATOR FAMILY gist_int8_ops USING gist ADD
-       FUNCTION 12 (int8, int8) gist_stratnum_btree (int2) ;
+       FUNCTION 12 (int8, int8) gist_stratnum_btree (int) ;
 
 ALTER OPERATOR FAMILY gist_float4_ops USING gist ADD
-       FUNCTION 12 (float4, float4) gist_stratnum_btree (int2) ;
+       FUNCTION 12 (float4, float4) gist_stratnum_btree (int) ;
 
 ALTER OPERATOR FAMILY gist_float8_ops USING gist ADD
-       FUNCTION 12 (float8, float8) gist_stratnum_btree (int2) ;
+       FUNCTION 12 (float8, float8) gist_stratnum_btree (int) ;
 
 ALTER OPERATOR FAMILY gist_timestamp_ops USING gist ADD
-       FUNCTION 12 (timestamp, timestamp) gist_stratnum_btree (int2) ;
+       FUNCTION 12 (timestamp, timestamp) gist_stratnum_btree (int) ;
 
 ALTER OPERATOR FAMILY gist_timestamptz_ops USING gist ADD
-       FUNCTION 12 (timestamptz, timestamptz) gist_stratnum_btree (int2) ;
+       FUNCTION 12 (timestamptz, timestamptz) gist_stratnum_btree (int) ;
 
 ALTER OPERATOR FAMILY gist_time_ops USING gist ADD
-       FUNCTION 12 (time, time) gist_stratnum_btree (int2) ;
+       FUNCTION 12 (time, time) gist_stratnum_btree (int) ;
 
 ALTER OPERATOR FAMILY gist_date_ops USING gist ADD
-       FUNCTION 12 (date, date) gist_stratnum_btree (int2) ;
+       FUNCTION 12 (date, date) gist_stratnum_btree (int) ;
 
 ALTER OPERATOR FAMILY gist_interval_ops USING gist ADD
-       FUNCTION 12 (interval, interval) gist_stratnum_btree (int2) ;
+       FUNCTION 12 (interval, interval) gist_stratnum_btree (int) ;
 
 ALTER OPERATOR FAMILY gist_cash_ops USING gist ADD
-       FUNCTION 12 (money, money) gist_stratnum_btree (int2) ;
+       FUNCTION 12 (money, money) gist_stratnum_btree (int) ;
 
 ALTER OPERATOR FAMILY gist_macaddr_ops USING gist ADD
-       FUNCTION 12 (macaddr, macaddr) gist_stratnum_btree (int2) ;
+       FUNCTION 12 (macaddr, macaddr) gist_stratnum_btree (int) ;
 
 ALTER OPERATOR FAMILY gist_text_ops USING gist ADD
-       FUNCTION 12 (text, text) gist_stratnum_btree (int2) ;
+       FUNCTION 12 (text, text) gist_stratnum_btree (int) ;
 
 ALTER OPERATOR FAMILY gist_bpchar_ops USING gist ADD
-       FUNCTION 12 (bpchar, bpchar) gist_stratnum_btree (int2) ;
+       FUNCTION 12 (bpchar, bpchar) gist_stratnum_btree (int) ;
 
 ALTER OPERATOR FAMILY gist_bytea_ops USING gist ADD
-       FUNCTION 12 (bytea, bytea) gist_stratnum_btree (int2) ;
+       FUNCTION 12 (bytea, bytea) gist_stratnum_btree (int) ;
 
 ALTER OPERATOR FAMILY gist_numeric_ops USING gist ADD
-       FUNCTION 12 (numeric, numeric) gist_stratnum_btree (int2) ;
+       FUNCTION 12 (numeric, numeric) gist_stratnum_btree (int) ;
 
 ALTER OPERATOR FAMILY gist_bit_ops USING gist ADD
-       FUNCTION 12 (bit, bit) gist_stratnum_btree (int2) ;
+       FUNCTION 12 (bit, bit) gist_stratnum_btree (int) ;
 
 ALTER OPERATOR FAMILY gist_vbit_ops USING gist ADD
-       FUNCTION 12 (varbit, varbit) gist_stratnum_btree (int2) ;
+       FUNCTION 12 (varbit, varbit) gist_stratnum_btree (int) ;
 
 ALTER OPERATOR FAMILY gist_inet_ops USING gist ADD
-       FUNCTION 12 (inet, inet) gist_stratnum_btree (int2) ;
+       FUNCTION 12 (inet, inet) gist_stratnum_btree (int) ;
 
 ALTER OPERATOR FAMILY gist_cidr_ops USING gist ADD
-       FUNCTION 12 (cidr, cidr) gist_stratnum_btree (int2) ;
+       FUNCTION 12 (cidr, cidr) gist_stratnum_btree (int) ;
 
 ALTER OPERATOR FAMILY gist_timetz_ops USING gist ADD
-       FUNCTION 12 (timetz, timetz) gist_stratnum_btree (int2) ;
+       FUNCTION 12 (timetz, timetz) gist_stratnum_btree (int) ;
 
 ALTER OPERATOR FAMILY gist_uuid_ops USING gist ADD
-       FUNCTION 12 (uuid, uuid) gist_stratnum_btree (int2) ;
+       FUNCTION 12 (uuid, uuid) gist_stratnum_btree (int) ;
 
 ALTER OPERATOR FAMILY gist_macaddr8_ops USING gist ADD
-       FUNCTION 12 (macaddr8, macaddr8) gist_stratnum_btree (int2) ;
+       FUNCTION 12 (macaddr8, macaddr8) gist_stratnum_btree (int) ;
 
 ALTER OPERATOR FAMILY gist_enum_ops USING gist ADD
-       FUNCTION 12 (anyenum, anyenum) gist_stratnum_btree (int2) ;
+       FUNCTION 12 (anyenum, anyenum) gist_stratnum_btree (int) ;
 
 ALTER OPERATOR FAMILY gist_bool_ops USING gist ADD
-       FUNCTION 12 (bool, bool) gist_stratnum_btree (int2) ;
+       FUNCTION 12 (bool, bool) gist_stratnum_btree (int) ;
index 5fd4cce27d05c65c6312739061334ce29286d3a3..fc6c7091795091ff0e3517b0c40377dbdf8944b9 100644 (file)
@@ -4,6 +4,7 @@
 #include "postgres.h"
 
 #include "access/stratnum.h"
+#include "nodes/primnodes.h"
 #include "utils/builtins.h"
 
 PG_MODULE_MAGIC;
@@ -60,19 +61,19 @@ gbt_decompress(PG_FUNCTION_ARGS)
 Datum
 gist_stratnum_btree(PG_FUNCTION_ARGS)
 {
-       StrategyNumber strat = PG_GETARG_UINT16(0);
+       CompareType cmptype = PG_GETARG_INT32(0);
 
-       switch (strat)
+       switch (cmptype)
        {
-               case RTEqualStrategyNumber:
+               case COMPARE_EQ:
                        PG_RETURN_UINT16(BTEqualStrategyNumber);
-               case RTLessStrategyNumber:
+               case COMPARE_LT:
                        PG_RETURN_UINT16(BTLessStrategyNumber);
-               case RTLessEqualStrategyNumber:
+               case COMPARE_LE:
                        PG_RETURN_UINT16(BTLessEqualStrategyNumber);
-               case RTGreaterStrategyNumber:
+               case COMPARE_GT:
                        PG_RETURN_UINT16(BTGreaterStrategyNumber);
-               case RTGreaterEqualStrategyNumber:
+               case COMPARE_GE:
                        PG_RETURN_UINT16(BTGreaterEqualStrategyNumber);
                default:
                        PG_RETURN_UINT16(InvalidStrategy);
index 9d80c6590d97777149f0883a4cc532d24309c0cb..dd0edaf4a206274ad3118f702f7d52d815e9d8bb 100644 (file)
@@ -1,11 +1,11 @@
 -- test stratnum support func
-SELECT gist_stratnum_btree(3::smallint);
+SELECT gist_stratnum_btree(7);
  gist_stratnum_btree 
 ---------------------
                    0
 (1 row)
 
-SELECT gist_stratnum_btree(18::smallint);
+SELECT gist_stratnum_btree(3);
  gist_stratnum_btree 
 ---------------------
                    3
index f58cdbe93da5238bd519ad972c0f20bb2ba3f710..75adddad8492583761418b9152b6afbe14531851 100644 (file)
@@ -1,3 +1,3 @@
 -- test stratnum support func
-SELECT gist_stratnum_btree(3::smallint);
-SELECT gist_stratnum_btree(18::smallint);
+SELECT gist_stratnum_btree(7);
+SELECT gist_stratnum_btree(3);
index 638d912dc2d11c2d030183a434d5710a62fd2989..99098ab2522831815f9b40d805656328bede09a2 100644 (file)
@@ -290,8 +290,8 @@ CREATE INDEX ON my_table USING GIST (my_inet_column inet_ops);
    The optional eleventh method <function>sortsupport</function> is used to
    speed up building a <acronym>GiST</acronym> index.
    The optional twelfth method <function>stratnum</function> is used to
-   translate well-known <literal>RT*StrategyNumber</literal>s (from
-   <filename>src/include/access/stratnum.h</filename>) into strategy numbers
+   translate compare types (from
+   <filename>src/include/nodes/primnodes.h</filename>) into strategy numbers
    used by the operator class.  This lets the core code look up operators for
    temporal constraint indexes.
  </para>
@@ -1173,8 +1173,8 @@ my_sortsupport(PG_FUNCTION_ARGS)
      <term><function>stratnum</function></term>
      <listitem>
       <para>
-       Given an <literal>RT*StrategyNumber</literal> value from
-       <filename>src/include/access/stratnum.h</filename>, returns a strategy
+       Given a <literal>CompareType</literal> value from
+       <filename>src/include/nodes/primnodes.h</filename>, returns a strategy
        number used by this operator class for matching functionality.  The
        function should return <literal>InvalidStrategy</literal> if the
        operator class has no matching strategy.
@@ -1184,7 +1184,7 @@ my_sortsupport(PG_FUNCTION_ARGS)
        This is used for temporal index constraints (i.e., <literal>PRIMARY
        KEY</literal> and <literal>UNIQUE</literal>).  If the operator class
        provides this function and it returns results for
-       <literal>RTEqualStrategyNumber</literal>, it can be used in the
+       <literal>COMPARE_EQ</literal>, it can be used in the
        non-<literal>WITHOUT OVERLAPS</literal> part(s) of an index constraint.
       </para>
 
@@ -1194,7 +1194,7 @@ my_sortsupport(PG_FUNCTION_ARGS)
 
 <programlisting>
 CREATE OR REPLACE FUNCTION my_stratnum(integer)
-RETURNS integer
+RETURNS smallint
 AS 'MODULE_PATHNAME'
 LANGUAGE C STRICT;
 </programlisting>
@@ -1209,12 +1209,12 @@ PG_FUNCTION_INFO_V1(my_stratnum);
 Datum
 my_stratnum(PG_FUNCTION_ARGS)
 {
-    StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(1);
+    CompareType cmptype = PG_GETARG_INT32(0);
     StrategyNumber ret = InvalidStrategy;
 
-    switch (strategy)
+    switch (cmptype)
     {
-        case RTEqualStrategyNumber:
+        case COMPARE_EQ:
             ret = BTEqualStrategyNumber;
     }
 
@@ -1226,9 +1226,9 @@ my_stratnum(PG_FUNCTION_ARGS)
       <para>
        One translation function is provided by
        <productname>PostgreSQL</productname>:
-       <literal>gist_stratnum_identity</literal> is for operator classes that
-       already use the <literal>RT*StrategyNumber</literal> constants.  It
-       returns whatever is passed to it.  The <literal>btree_gist</literal>
+       <literal>gist_stratnum_common</literal> is for operator classes that
+       use the <literal>RT*StrategyNumber</literal> constants.
+       The <literal>btree_gist</literal>
        extension defines a second translation function,
        <literal>gist_stratnum_btree</literal>, for operator classes that use
        the <literal>BT*StrategyNumber</literal> constants.
index 3a19dab15e031d865ab71de45bf0705a1d45a4fa..053619624950c26234ead7b7466849b5ece4cb32 100644 (file)
       </row>
       <row>
        <entry><function>stratnum</function></entry>
-       <entry>translate well-known strategy numbers to ones
+       <entry>translate compare types to strategy numbers
         used by the operator class (optional)</entry>
        <entry>12</entry>
       </row>
index a2fcfbe480717539be528e10a163b07641490c12..48db718b904032ad5ed7b9527f63c2532055d31b 100644 (file)
@@ -1058,27 +1058,44 @@ gistGetFakeLSN(Relation rel)
 }
 
 /*
- * Returns the same number that was received.
- *
- * This is for GiST opclasses that use the RT*StrategyNumber constants.
+ * This is a stratnum support function for GiST opclasses that use the
+ * RT*StrategyNumber constants.
  */
 Datum
-gist_stratnum_identity(PG_FUNCTION_ARGS)
+gist_stratnum_common(PG_FUNCTION_ARGS)
 {
-       StrategyNumber strat = PG_GETARG_UINT16(0);
+       CompareType cmptype = PG_GETARG_INT32(0);
 
-       PG_RETURN_UINT16(strat);
+       switch (cmptype)
+       {
+               case COMPARE_EQ:
+                       PG_RETURN_UINT16(RTEqualStrategyNumber);
+               case COMPARE_LT:
+                       PG_RETURN_UINT16(RTLessStrategyNumber);
+               case COMPARE_LE:
+                       PG_RETURN_UINT16(RTLessEqualStrategyNumber);
+               case COMPARE_GT:
+                       PG_RETURN_UINT16(RTGreaterStrategyNumber);
+               case COMPARE_GE:
+                       PG_RETURN_UINT16(RTGreaterEqualStrategyNumber);
+               case COMPARE_OVERLAP:
+                       PG_RETURN_UINT16(RTOverlapStrategyNumber);
+               case COMPARE_CONTAINED_BY:
+                       PG_RETURN_UINT16(RTContainedByStrategyNumber);
+               default:
+                       PG_RETURN_UINT16(InvalidStrategy);
+       }
 }
 
 /*
- * Returns the opclass's private stratnum used for the given strategy.
+ * Returns the opclass's private stratnum used for the given compare type.
  *
  * Calls the opclass's GIST_STRATNUM_PROC support function, if any,
  * and returns the result.
  * Returns InvalidStrategy if the function is not defined.
  */
 StrategyNumber
-GistTranslateStratnum(Oid opclass, StrategyNumber strat)
+GistTranslateStratnum(Oid opclass, CompareType cmptype)
 {
        Oid                     opfamily;
        Oid                     opcintype;
@@ -1095,6 +1112,6 @@ GistTranslateStratnum(Oid opclass, StrategyNumber strat)
                return InvalidStrategy;
 
        /* Ask the translation function */
-       result = OidFunctionCall1Coll(funcid, InvalidOid, UInt16GetDatum(strat));
+       result = OidFunctionCall1Coll(funcid, InvalidOid, Int32GetDatum(cmptype));
        return DatumGetUInt16(result);
 }
index 499ed8c8748ac4346c50d6004ba1d948ee3bba03..bb86b5594868e2c75905629cedc7ed7d103eafe9 100644 (file)
@@ -148,7 +148,7 @@ gistvalidate(Oid opclassoid)
                                break;
                        case GIST_STRATNUM_PROC:
                                ok = check_amproc_signature(procform->amproc, INT2OID, true,
-                                                                                       1, 1, INT2OID);
+                       &