Fix arrays comparison in CompareOpclassOptions()
authorAlexander Korotkov <[email protected]>
Mon, 11 Nov 2024 23:44:20 +0000 (01:44 +0200)
committerAlexander Korotkov <[email protected]>
Mon, 11 Nov 2024 23:54:35 +0000 (01:54 +0200)
The current code calls array_eq() and does not provide FmgrInfo.  This commit
provides initialization of FmgrInfo and uses C collation as the safe option
for text comparison because we don't know anything about the semantics of
opclass options.

Backpatch to 13, where opclass options were introduced.

Reported-by: Nicolas Maus
Discussion: https://postgr.es/m/18692-72ea398df3ec6712%40postgresql.org
Backpatch-through: 13

contrib/pg_trgm/expected/pg_trgm.out
contrib/pg_trgm/sql/pg_trgm.sql
src/backend/commands/indexcmds.c

index 20141ce7f3d89c10224a3c38ead7df1f723ef81f..8b5dcf90d1864da95457f4184eee62d397e3cd17 100644 (file)
@@ -2372,6 +2372,9 @@ ERROR:  value 2025 out of bounds for option "siglen"
 DETAIL:  Valid values are between "1" and "2024".
 create index trgm_idx on test_trgm using gist (t gist_trgm_ops(siglen=2024));
 set enable_seqscan=off;
+-- check index compatibility handling when opclass option is specified
+alter table test_trgm alter column t type varchar(768);
+alter table test_trgm alter column t type text;
 select t,similarity(t,'qwertyu0988') as sml from test_trgm where t % 'qwertyu0988' order by sml desc, t;
       t      |   sml    
 -------------+----------
index 6a9da24d5a72b3c9928deedd9d74540fe7fa35fb..340c9891899f051f19a709cdf53673dcb4a16546 100644 (file)
@@ -52,6 +52,10 @@ create index trgm_idx on test_trgm using gist (t gist_trgm_ops(siglen=2025));
 create index trgm_idx on test_trgm using gist (t gist_trgm_ops(siglen=2024));
 set enable_seqscan=off;
 
+-- check index compatibility handling when opclass option is specified
+alter table test_trgm alter column t type varchar(768);
+alter table test_trgm alter column t type text;
+
 select t,similarity(t,'qwertyu0988') as sml from test_trgm where t % 'qwertyu0988' order by sml desc, t;
 select t,similarity(t,'gwertyu0988') as sml from test_trgm where t % 'gwertyu0988' order by sml desc, t;
 select t,similarity(t,'gwertyu1988') as sml from test_trgm where t % 'gwertyu1988' order by sml desc, t;
index 99c536891a7af29e2faa6e65104b24148e6e105f..c2904cdb1a77f99e07d8fa4afbac9e5b546d6e60 100644 (file)
@@ -26,6 +26,7 @@
 #include "catalog/index.h"
 #include "catalog/indexing.h"
 #include "catalog/pg_am.h"
+#include "catalog/pg_collation.h"
 #include "catalog/pg_constraint.h"
 #include "catalog/pg_inherits.h"
 #include "catalog/pg_opclass.h"
@@ -349,10 +350,12 @@ static bool
 CompareOpclassOptions(Datum *opts1, Datum *opts2, int natts)
 {
    int         i;
+   FmgrInfo    fm;
 
    if (!opts1 && !opts2)
        return true;
 
+   fmgr_info(F_ARRAY_EQ, &fm);
    for (i = 0; i < natts; i++)
    {
        Datum       opt1 = opts1 ? opts1[i] : (Datum) 0;
@@ -368,8 +371,12 @@ CompareOpclassOptions(Datum *opts1, Datum *opts2, int natts)
        else if (opt2 == (Datum) 0)
            return false;
 
-       /* Compare non-NULL text[] datums. */
-       if (!DatumGetBool(DirectFunctionCall2(array_eq, opt1, opt2)))
+       /*
+        * Compare non-NULL text[] datums.  Use C collation to enforce binary
+        * equivalence of texts, because we don't know anything about the
+        * semantics of opclass options.
+        */
+       if (!DatumGetBool(FunctionCall2Coll(&fm, C_COLLATION_OID, opt1, opt2)))
            return false;
    }