Unbreak index optimization for LIKE on bytea
authorPeter Eisentraut <peter@eisentraut.org>
Mon, 15 Apr 2019 07:26:23 +0000 (09:26 +0200)
committerPeter Eisentraut <peter@eisentraut.org>
Mon, 15 Apr 2019 07:29:17 +0000 (09:29 +0200)
The same code is used to handle both text and bytea, but bytea is not
collation-aware, so we shouldn't call get_collation_isdeterministic()
in that case, since that will error out with an invalid collation.

Reported-by: Jeevan Chalke <jeevan.chalke@enterprisedb.com>
Discussion: https://www.postgresql.org/message-id/flat/CAM2%2B6%3DWaf3qJ1%3DyVTUH8_yG-SC0xcBMY%2BSFLhvKKNnWNXSUDBw%40mail.gmail.com

src/backend/utils/adt/like_support.c
src/test/regress/expected/strings.out
src/test/regress/sql/strings.sql

index a65e63736c45db897ab6c80daa64639e51289df4..7528c80f7c342e00caa8e5c1360a69d8d58c87bf 100644 (file)
@@ -267,8 +267,10 @@ match_pattern_prefix(Node *leftop,
         * precise error messages.)  (It should be possible to support at least
         * Pattern_Prefix_Exact, but no point as along as the actual
         * pattern-matching implementations don't support it.)
+        *
+        * expr_coll is not set for a non-collation-aware data type such as bytea.
         */
-       if (!get_collation_isdeterministic(expr_coll))
+       if (expr_coll && !get_collation_isdeterministic(expr_coll))
                return NIL;
 
        /*
index 2f5f58273ab5ac057d574729f631601c5279b9b4..7c7f8726fcca7947dfe88050d2f176dc8440957d 100644 (file)
@@ -1101,6 +1101,22 @@ SELECT 'jack' LIKE '%____%' AS t;
  t
 (1 row)
 
+--
+-- basic tests of LIKE with indexes
+--
+CREATE TABLE texttest (a text PRIMARY KEY, b int);
+SELECT * FROM texttest WHERE a LIKE '%1%';
+ a | b 
+---+---
+(0 rows)
+
+CREATE TABLE byteatest (a bytea PRIMARY KEY, b int);
+SELECT * FROM byteatest WHERE a LIKE '%1%';
+ a | b 
+---+---
+(0 rows)
+
+DROP TABLE texttest, byteatest;
 --
 -- test implicit type conversion
 --
index 1f4cd88a18ffa56e3b9227dc8017607044463633..9cd2bc5d7e8dbd0f9a48852d1b6fb7a3ac0a55ac 100644 (file)
@@ -323,6 +323,19 @@ SELECT 'foo' LIKE '%__' as t, 'foo' LIKE '%___' as t, 'foo' LIKE '%____' as f;
 SELECT 'jack' LIKE '%____%' AS t;
 
 
+--
+-- basic tests of LIKE with indexes
+--
+
+CREATE TABLE texttest (a text PRIMARY KEY, b int);
+SELECT * FROM texttest WHERE a LIKE '%1%';
+
+CREATE TABLE byteatest (a bytea PRIMARY KEY, b int);
+SELECT * FROM byteatest WHERE a LIKE '%1%';
+
+DROP TABLE texttest, byteatest;
+
+
 --
 -- test implicit type conversion
 --