Fix an issue with index scan using pg_trgm due to char signedness on different archit...
authorMasahiko Sawada <[email protected]>
Fri, 21 Feb 2025 18:27:39 +0000 (10:27 -0800)
committerMasahiko Sawada <[email protected]>
Fri, 21 Feb 2025 18:27:39 +0000 (10:27 -0800)
GIN and GiST indexes utilizing pg_trgm's opclasses store sorted
trigrams within index tuples. When comparing and sorting each trigram,
pg_trgm treats each character as a 'char[3]' type in C. However, the
char type in C can be interpreted as either signed char or unsigned
char, depending on the platform, if the signedness is not explicitly
specified. Consequently, during replication between different CPU
architectures, there was an issue where index scans on standby servers
could not locate matching index tuples due to the differing treatment
of character signedness.

This change introduces comparison functions for trgm that explicitly
handle signed char and unsigned char. The appropriate comparison
function will be dynamically selected based on the character
signedness stored in the control file. Therefore, upgraded clusters
can utilize the indexes without rebuilding, provided the cluster
upgrade occurs on platforms with the same character signedness as the
original cluster initialization.

The default char signedness information was introduced in 44fe30fdab6,
so no backpatch.

Reviewed-by: Noah Misch <[email protected]>
Discussion: https://postgr.es/m/CB11ADBC-0C3F-4FE0-A678-666EE80CBB07%40amazon.com

contrib/pg_trgm/trgm.h
contrib/pg_trgm/trgm_op.c

index 10827563694fed30d4a83549dabe24be72874e50..ca017585369ad7ef870b02dba68a18e45108c8e3 100644 (file)
 
 typedef char trgm[3];
 
-#define CMPCHAR(a,b) ( ((a)==(b)) ? 0 : ( ((a)<(b)) ? -1 : 1 ) )
-#define CMPPCHAR(a,b,i)  CMPCHAR( *(((const char*)(a))+i), *(((const char*)(b))+i) )
-#define CMPTRGM(a,b) ( CMPPCHAR(a,b,0) ? CMPPCHAR(a,b,0) : ( CMPPCHAR(a,b,1) ? CMPPCHAR(a,b,1) : CMPPCHAR(a,b,2) ) )
-
 #define CPTRGM(a,b) do {               \
    *(((char*)(a))+0) = *(((char*)(b))+0);  \
    *(((char*)(a))+1) = *(((char*)(b))+1);  \
    *(((char*)(a))+2) = *(((char*)(b))+2);  \
 } while(0)
+extern int (*CMPTRGM) (const void *a, const void *b);
 
 #define ISWORDCHR(c)   (t_isalnum(c))
 #define ISPRINTABLECHAR(a) ( isascii( *(unsigned char*)(a) ) && (isalnum( *(unsigned char*)(a) ) || *(unsigned char*)(a)==' ') )
index d0833b3e4a12a7f8e8c62976c1d027fe8da68217..94b9015fd676765b7fe3cf5324b058862b4d4972 100644 (file)
@@ -42,6 +42,9 @@ PG_FUNCTION_INFO_V1(strict_word_similarity_commutator_op);
 PG_FUNCTION_INFO_V1(strict_word_similarity_dist_op);
 PG_FUNCTION_INFO_V1(strict_word_similarity_dist_commutator_op);
 
+static int CMPTRGM_CHOOSE(const void *a, const void *b);
+int            (*CMPTRGM) (const void *a, const void *b) = CMPTRGM_CHOOSE;
+
 /* Trigram with position */
 typedef struct
 {
@@ -107,6 +110,47 @@ _PG_init(void)
    MarkGUCPrefixReserved("pg_trgm");
 }
 
+#define CMPCHAR(a,b) ( ((a)==(b)) ? 0 : ( ((a)<(b)) ? -1 : 1 ) )
+
+/*
+ * Functions for comparing two trgms while treating each char as "signed char" or
+ * "unsigned char".
+ */
+static inline int
+CMPTRGM_SIGNED(const void *a, const void *b)
+{
+#define CMPPCHAR_S(a,b,i)  CMPCHAR( *(((const signed char*)(a))+i), *(((const signed char*)(b))+i) )
+
+   return CMPPCHAR_S(a, b, 0) ? CMPPCHAR_S(a, b, 0)
+       : (CMPPCHAR_S(a, b, 1) ? CMPPCHAR_S(a, b, 1)
+          : CMPPCHAR_S(a, b, 2));
+}
+
+static inline int
+CMPTRGM_UNSIGNED(const void *a, const void *b)
+{
+#define CMPPCHAR_UNS(a,b,i)  CMPCHAR( *(((const unsigned char*)(a))+i), *(((const unsigned char*)(b))+i) )
+
+   return CMPPCHAR_UNS(a, b, 0) ? CMPPCHAR_UNS(a, b, 0)
+       : (CMPPCHAR_UNS(a, b, 1) ? CMPPCHAR_UNS(a, b, 1)
+          : CMPPCHAR_UNS(a, b, 2));
+}
+
+/*
+ * This gets called on the first call. It replaces the function pointer so
+ * that subsequent calls are routed directly to the chosen implementation.
+ */
+static int
+CMPTRGM_CHOOSE(const void *a, const void *b)
+{
+   if (GetDefaultCharSignedness())
+       CMPTRGM = CMPTRGM_SIGNED;
+   else
+       CMPTRGM = CMPTRGM_UNSIGNED;
+
+   return CMPTRGM(a, b);
+}
+
 /*
  * Deprecated function.
  * Use "pg_trgm.similarity_threshold" GUC variable instead of this function.