Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id EBC3D358181 for ; Fri, 9 Dec 2011 15:38:49 -0400 (AST) Received: from col0-omc1-s5.col0.hotmail.com ([65.55.34.15]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RZ6HQ-0001lP-Jk for pgsql-bugs@postgresql.org; Fri, 09 Dec 2011 19:38:49 +0000 Received: from COL116-W33 ([65.55.34.7]) by col0-omc1-s5.col0.hotmail.com with Microsoft SMTPSVC(6.0.3790.4675); Fri, 9 Dec 2011 11:38:35 -0800 Message-ID: X-Originating-IP: [38.99.150.66] From: John Lumby To: Subject: LIKE predicate and ERROR: 42P22: could not determine which collation to use for string comparison - HINT: Use the COLLATE clause ... Date: Fri, 9 Dec 2011 14:38:34 -0500 Importance: Normal Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginalArrivalTime: 09 Dec 2011 19:38:35.0884 (UTC) FILETIME=[246ED2C0:01CCB6AA] X-Pg-Spam-Score: -3.1 (---) X-Archive-Number: 201112/61 X-Sequence-Number: 31872 postgresql 9.1.2 running on linux debian squeeze on 32-bit x86 I have encountered behaviour relating to subject line which I'm fairly sure= is a bug=2C=A0 although I'm not sure exactly what. My database was created with LC_LOCALE C=2C=A0=A0 and loaded using Pg-9.1.1= . =A0 it looks like psql -d proteusdb -c "select datname=2C datdba=2C encoding=2C datcollate=2C= datctype=2C datistemplate=2C datallowconn=2C datconnlimit=2C datlastsysoid= =2C datfrozenxid=2C dattablespace from pg_database where datname =3D 'prote= usdb'"=3B =A0 datname=A0 | datdba | encoding | datcollate | datctype | datistemplate = | datallowconn | datconnlimit | datlastsysoid | datfrozenxid | dattablespac= e=20 -----------+--------+----------+------------+----------+---------------+---= -----------+--------------+---------------+--------------+--------------- =A0proteusdb |=A0=A0=A0=A0 10 |=A0=A0=A0=A0=A0=A0=A0 6 | C=A0=A0=A0=A0=A0= =A0=A0=A0=A0 | C=A0=A0=A0=A0=A0=A0=A0 | f=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0 | t=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 |=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 -1= |=A0=A0=A0=A0=A0=A0=A0=A0 11910 |=A0=A0=A0=A0=A0=A0=A0=A0=A0 670 |=A0=A0= =A0=A0=A0=A0=A0=A0=A0 1663 (1 row) =A0I have a table which as a column named discriminator : CREATE TABLE entity ( =A0=A0=A0 id bigint DEFAULT nextval('entity_id_seq'::regclass) NOT NULL=2C =A0=A0=A0 discriminator character varying(255) NOT NULL=2C =A0=A0=A0 version integer NOT NULL=2C =A0=A0=A0 inherit_right boolean=2C =A0=A0=A0 name text=2C =A0=A0=A0 parent_id bigint=2C =A0=A0=A0 association_id bigint=2C =A0=A0=A0 association2_id bigint=2C =A0=A0=A0 long1 bigint=2C =A0=A0=A0 long2 bigint )=3B which shows up looking like this in the catalog psql -d proteusdb -c "select N.nspname =2C C.relname=2C A.attrelid =2C A.at= tname =2C A.atttypid =2C T.typname =2C L.collname from pg_attribute A=2C pg= _type T=2C pg_class C =2C pg_namespace N =2C pg_collation L where C.oid =3D= A.attrelid and T.oid =3D A.atttypid and A.attcollation =3D L.oid and C.rel= namespace =3D N.oid and N.nspname =3D 'public' and C.relname =3D 'entity' a= nd A.attname =3D 'discriminator'"=3B =A0nspname | relname | attrelid |=A0=A0=A0 attname=A0=A0=A0 | atttypid | ty= pname | collname=20 ---------+---------+----------+---------------+----------+---------+-------= --- =A0 public=A0 | entity=A0 |=A0=A0=A0 25349 | discriminator |=A0=A0=A0=A0 10= 43 | varchar | default (1 row) and a query like so : psql -d proteusdb -c "select count(*) from entity=A0 as e1 where ( e1.assoc= iation_id IN ( select id from entity as e2 where e2.parent_id=3D3043508)=A0= =A0 OR e1.association_id=3D3043508 ) and e1.discriminator LIKE 'DEPLOY%'"= =3B ERROR:=A0 could not determine which collation to use for string comparison HINT:=A0 Use the COLLATE clause to set the collation explicitly. explain gives a clue what is going on : psql -d proteusdb -c "explain select count(*) from entity=A0 as e1 where ( = e1.association_id IN ( select id from entity as e2 where e2.parent_id=3D304= 3508)=A0=A0 OR e1.association_id=3D3043508 ) and e1.discriminator LIKE 'DEP= LOY%'"=3B =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0 QUERY PLAN=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=20 ---------------------------------------------------------------------------= ------------------------------------------ =A0Aggregate=A0 (cost=3D85045.70..85045.71 rows=3D1 width=3D0) =A0=A0 ->=A0 Index Scan using entity_id_association_id_discriminator on ent= ity e1=A0 (cost=3D523.55..85045.61 rows=3D36 width=3D0) =A0=A0=A0=A0=A0=A0=A0=A0 Index Cond: (((discriminator)::text >=3D 'DEPLOY':= :text) AND ((discriminator)::text < 'DEPLOZ'::text)) =A0=A0=A0=A0=A0=A0=A0=A0 Filter: (((discriminator)::text ~~ 'DEPLOY%'::text= ) AND ((hashed SubPlan 1) OR (association_id =3D 3043508))) =A0=A0=A0=A0=A0=A0=A0=A0 SubPlan 1 =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 ->=A0 Index Scan using entity_parent_id on e= ntity e2=A0 (cost=3D0.00..522.84 rows=3D281 width=3D8) =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 Index Cond: (parent_id =3D= 3043508) (7 rows) So Pg has rewritten my LIKE pred to add the pair of range predicates so as = to be able to exploit my index ... Good. However=A0 ---=A0=A0 It has then discovered that there is a mismatch betwee= n the collation of the column (default) and the constant (I assume C=2C=A0 = inherited from the database). Well=A0=A0 --=A0=A0=A0 I am not an expert on collation=2C=A0=A0 but I am fa= irly sure that the semantics of the LIKE predicate do not involve order=2C= =A0=A0 so an error relating to ordering or collation should be impossible.= =A0=A0=A0=A0 LIKE involves only exact (equality) matching of parts of the c= olumn value against the pattern.=A0=A0=A0=A0 If the query rewriter has sati= sified itself that it is a correct transformation of the query to add the t= wo range predicates=2C=A0=A0 then it must=A0 (should?)=A0 have checked that= any mismatch of collation does not affect correctness=2C and therefore no = other part of postgres (planner=2C=A0 runtime) should then object on those = grounds. So=A0 - I *think* there is a=A0 bug=2C=A0=A0 either that the query should n= ot have been rewritten=A0 (if collation does indeed make the rewrite incorr= ect)=2C=A0 or else it should have planned and executed the rewritten query. I re-ran this on PG 9.1.2 to check and the same happens there as well. Hoping that some expert can make a ruling on this. John Lumby =