/*
 * This test is for Linux/glibc systems and assumes that a full set of
 * locales is installed.  It must be run in a UTF-8 locale.
 */
SET client_encoding TO UTF8;
CREATE TABLE collate_test1 (
    a int,
    b text COLLATE "en_US.utf8" NOT NULL
);
\d collate_test1
          Table "public.collate_test1"
 Column |  Type   |          Modifiers          
--------+---------+-----------------------------
 a      | integer | 
 b      | text    | collate en_US.utf8 not null

CREATE TABLE collate_test_fail (
    a int,
    b text COLLATE "ja_JP.eucjp"
);
ERROR:  collation "ja_JP.eucjp" for encoding "UTF8" does not exist
LINE 3:     b text COLLATE "ja_JP.eucjp"
                   ^
CREATE TABLE collate_test_fail (
    a int,
    b text COLLATE "foo"
);
ERROR:  collation "foo" for encoding "UTF8" does not exist
LINE 3:     b text COLLATE "foo"
                   ^
CREATE TABLE collate_test_fail (
    a int COLLATE "en_US.utf8",
    b text
);
ERROR:  collations are not supported by type integer
LINE 2:     a int COLLATE "en_US.utf8",
                  ^
CREATE TABLE collate_test_like (
    LIKE collate_test1
);
\d collate_test_like
        Table "public.collate_test_like"
 Column |  Type   |          Modifiers          
--------+---------+-----------------------------
 a      | integer | 
 b      | text    | collate en_US.utf8 not null

CREATE TABLE collate_test2 (
    a int,
    b text COLLATE "sv_SE.utf8"
);
CREATE TABLE collate_test3 (
    a int,
    b text COLLATE "C"
);
INSERT INTO collate_test1 VALUES (1, 'abc'), (2, 'äbc'), (3, 'bbc'), (4, 'ABC');
INSERT INTO collate_test2 SELECT * FROM collate_test1;
INSERT INTO collate_test3 SELECT * FROM collate_test1;
SELECT * FROM collate_test1 WHERE b >= 'bbc';
 a |  b  
---+-----
 3 | bbc
(1 row)

SELECT * FROM collate_test2 WHERE b >= 'bbc';
 a |  b  
---+-----
 2 | äbc
 3 | bbc
(2 rows)

SELECT * FROM collate_test3 WHERE b >= 'bbc';
 a |  b  
---+-----
 2 | äbc
 3 | bbc
(2 rows)

SELECT * FROM collate_test3 WHERE b >= 'BBC';
 a |  b  
---+-----
 1 | abc
 2 | äbc
 3 | bbc
(3 rows)

SELECT * FROM collate_test1 WHERE b COLLATE "C" >= 'bbc';
 a |  b  
---+-----
 2 | äbc
 3 | bbc
(2 rows)

SELECT * FROM collate_test1 WHERE b >= 'bbc' COLLATE "C";
 a |  b  
---+-----
 2 | äbc
 3 | bbc
(2 rows)

SELECT * FROM collate_test1 WHERE b COLLATE "C" >= 'bbc' COLLATE "C";
 a |  b  
---+-----
 2 | äbc
 3 | bbc
(2 rows)

SELECT * FROM collate_test1 WHERE b COLLATE "C" >= 'bbc' COLLATE "en_US.utf8";
ERROR:  collation mismatch between explicit collations "C" and "en_US.utf8"
LINE 1: ...ELECT * FROM collate_test1 WHERE b COLLATE "C" >= 'bbc' COLL...
                                                             ^
SELECT * FROM collate_test1 WHERE b COLLATE "C" >= 'bbc' COLLATE "en_US";
ERROR:  collation mismatch between explicit collations "C" and "en_US"
LINE 1: ...ELECT * FROM collate_test1 WHERE b COLLATE "C" >= 'bbc' COLL...
                                                             ^
CREATE DOMAIN testdomain_sv AS text COLLATE "sv_SE.utf8";
CREATE DOMAIN testdomain_i AS int COLLATE "sv_SE.utf8"; -- fails
ERROR:  collations are not supported by type integer
CREATE TABLE collate_test4 (
    a int,
    b testdomain_sv
);
INSERT INTO collate_test4 SELECT * FROM collate_test1;
SELECT a, b FROM collate_test4 ORDER BY b;
 a |  b  
---+-----
 1 | abc
 4 | ABC
 3 | bbc
 2 | äbc
(4 rows)

CREATE TABLE collate_test5 (
    a int,
    b testdomain_sv COLLATE "en_US.utf8"
);
INSERT INTO collate_test5 SELECT * FROM collate_test1;
SELECT a, b FROM collate_test5 ORDER BY b;
 a |  b  
---+-----
 1 | abc
 4 | ABC
 2 | äbc
 3 | bbc
(4 rows)

SELECT a, b FROM collate_test1 ORDER BY b;
 a |  b  
---+-----
 1 | abc
 4 | ABC
 2 | äbc
 3 | bbc
(4 rows)

SELECT a, b FROM collate_test2 ORDER BY b;
 a |  b  
---+-----
 1 | abc
 4 | ABC
 3 | bbc
 2 | äbc
(4 rows)

SELECT a, b FROM collate_test3 ORDER BY b;
 a |  b  
---+-----
 4 | ABC
 1 | abc
 3 | bbc
 2 | äbc
(4 rows)

SELECT a, b FROM collate_test1 ORDER BY b COLLATE "C";
 a |  b  
---+-----
 4 | ABC
 1 | abc
 3 | bbc
 2 | äbc
(4 rows)

-- star expansion
SELECT * FROM collate_test1 ORDER BY b;
 a |  b  
---+-----
 1 | abc
 4 | ABC
 2 | äbc
 3 | bbc
(4 rows)

SELECT * FROM collate_test2 ORDER BY b;
 a |  b  
---+-----
 1 | abc
 4 | ABC
 3 | bbc
 2 | äbc
(4 rows)

SELECT * FROM collate_test3 ORDER BY b;
 a |  b  
---+-----
 4 | ABC
 1 | abc
 3 | bbc
 2 | äbc
(4 rows)

-- constant expression folding
SELECT 'bbc' COLLATE "en_US.utf8" > 'äbc' COLLATE "en_US.utf8" AS "true";
 true 
------
 t
(1 row)

SELECT 'bbc' COLLATE "sv_SE.utf8" > 'äbc' COLLATE "sv_SE.utf8" AS "false";
 false 
-------
 f
(1 row)

-- upper/lower
CREATE TABLE collate_test10 (
    a int,
    x text COLLATE "en_US.utf8",
    y text COLLATE "tr_TR.utf8"
);
INSERT INTO collate_test10 VALUES (1, 'hij', 'hij'), (2, 'HIJ', 'HIJ');
SELECT a, lower(x), lower(y), upper(x), upper(y), initcap(x), initcap(y) FROM collate_test10;
 a | lower | lower | upper | upper | initcap | initcap 
---+-------+-------+-------+-------+---------+---------
 1 | hij   | hij   | HIJ   | HİJ   | Hij     | Hij
 2 | hij   | hıj   | HIJ   | HIJ   | Hij     | Hıj
(2 rows)

SELECT a, lower(x COLLATE "C"), lower(y COLLATE "C") FROM collate_test10;
 a | lower | lower 
---+-------+-------
 1 | hij   | hij
 2 | hij   | hij
(2 rows)

SELECT a, x, y FROM collate_test10 ORDER BY lower(y), a;
 a |  x  |  y  
---+-----+-----
 2 | HIJ | HIJ
 1 | hij | hij
(2 rows)

-- LIKE/ILIKE
SELECT * FROM collate_test1 WHERE b LIKE 'abc';
 a |  b  
---+-----
 1 | abc
(1 row)

SELECT * FROM collate_test1 WHERE b LIKE 'abc%';
 a |  b  
---+-----
 1 | abc
(1 row)

SELECT * FROM collate_test1 WHERE b LIKE '%bc%';
 a |  b  
---+-----
 1 | abc
 2 | äbc
 3 | bbc
(3 rows)

SELECT * FROM collate_test1 WHERE b ILIKE 'abc';
 a |  b  
---+-----
 1 | abc
 4 | ABC
(2 rows)

SELECT * FROM collate_test1 WHERE b ILIKE 'abc%';
 a |  b  
---+-----
 1 | abc
 4 | ABC
(2 rows)

SELECT * FROM collate_test1 WHERE b ILIKE '%bc%';
 a |  b  
---+-----
 1 | abc
 2 | äbc
 3 | bbc
 4 | ABC
(4 rows)

SELECT 'Türkiye' COLLATE "en_US.utf8" ILIKE '%KI%' AS "true";
 true 
------
 t
(1 row)

SELECT 'Türkiye' COLLATE "tr_TR.utf8" ILIKE '%KI%' AS "false";
 false 
-------
 f
(1 row)

-- The following actually exercises the selectivity estimation for ILIKE.
SELECT relname FROM pg_class WHERE relname ILIKE 'abc%';
 relname 
---------
(0 rows)

-- to_char
SET lc_time TO 'tr_TR.utf8';
SELECT to_char(date '2010-04-01', 'DD TMMON YYYY');
   to_char   
-------------
 01 NIS 2010
(1 row)

SELECT to_char(date '2010-04-01', 'DD TMMON YYYY' COLLATE "tr_TR.utf8");
   to_char   
-------------
 01 NİS 2010
(1 row)

-- backwards parsing
CREATE VIEW collview1 AS SELECT * FROM collate_test1 WHERE b COLLATE "C" >= 'bbc';
CREATE VIEW collview2 AS SELECT a, b FROM collate_test1 ORDER BY b COLLATE "C";
CREATE VIEW collview3 AS SELECT a, lower((x || x) COLLATE "C") FROM collate_test10;
SELECT table_name, view_definition FROM information_schema.views
  WHERE table_name LIKE 'collview%' ORDER BY 1;
 table_name |                                                  view_definition                                                   
------------+--------------------------------------------------------------------------------------------------------------------
 collview1  | SELECT collate_test1.a, collate_test1.b FROM collate_test1 WHERE ((collate_test1.b COLLATE "C") >= 'bbc'::text);
 collview2  | SELECT collate_test1.a, collate_test1.b FROM collate_test1 ORDER BY (collate_test1.b COLLATE "C");
 collview3  | SELECT collate_test10.a, lower(((collate_test10.x || collate_test10.x) COLLATE "C")) AS lower FROM collate_test10;
(3 rows)

-- collation propagation in various expression type
SELECT a, coalesce(b, 'foo') FROM collate_test1 ORDER BY 2;
 a | coalesce 
---+----------
 1 | abc
 4 | ABC
 2 | äbc
 3 | bbc
(4 rows)

SELECT a, coalesce(b, 'foo') FROM collate_test2 ORDER BY 2;
 a | coalesce 
---+----------
 1 | abc
 4 | ABC
 3 | bbc
 2 | äbc
(4 rows)

SELECT a, coalesce(b, 'foo') FROM collate_test3 ORDER BY 2;
 a | coalesce 
---+----------
 4 | ABC
 1 | abc
 3 | bbc
 2 | äbc
(4 rows)

SELECT a, lower(coalesce(x, 'foo')), lower(coalesce(y, 'foo')) FROM collate_test10;
 a | lower | lower 
---+-------+-------
 1 | hij   | hij
 2 | hij   | hıj
(2 rows)

SELECT a, b, greatest(b, 'CCC') FROM collate_test1 ORDER BY 3;
 a |  b  | greatest 
---+-----+----------
 1 | abc | CCC
 2 | äbc | CCC
 3 | bbc | CCC
 4 | ABC | CCC
(4 rows)

SELECT a, b, greatest(b, 'CCC') FROM collate_test2 ORDER BY 3;
 a |  b  | greatest 
---+-----+----------
 1 | abc | CCC
 3 | bbc | CCC
 4 | ABC | CCC
 2 | äbc | äbc
(4 rows)

SELECT a, b, greatest(b, 'CCC') FROM collate_test3 ORDER BY 3;
 a |  b  | greatest 
---+-----+----------
 4 | ABC | CCC
 1 | abc | abc
 3 | bbc | bbc
 2 | äbc | äbc
(4 rows)

SELECT a, x, y, lower(greatest(x, 'foo')), lower(greatest(y, 'foo')) FROM collate_test10;
 a |  x  |  y  | lower | lower 
---+-----+-----+-------+-------
 1 | hij | hij | hij   | hij
 2 | HIJ | HIJ | hij   | hıj
(2 rows)

SELECT a, nullif(b, 'abc') FROM collate_test1 ORDER BY 2;
 a | nullif 
---+--------
 4 | ABC
 2 | äbc
 3 | bbc
 1 | 
(4 rows)

SELECT a, nullif(b, 'abc') FROM collate_test2 ORDER BY 2;
 a | nullif 
---+--------
 4 | ABC
 3 | bbc
 2 | äbc
 1 | 
(4 rows)

SELECT a, nullif(b, 'abc') FROM collate_test3 ORDER BY 2;
 a | nullif 
---+--------
 4 | ABC
 3 | bbc
 2 | äbc
 1 | 
(4 rows)

SELECT a, lower(nullif(x, 'foo')), lower(nullif(y, 'foo')) FROM collate_test10;
 a | lower | lower 
---+-------+-------
 1 | hij   | hij
 2 | hij   | hıj
(2 rows)

SELECT a, CASE b WHEN 'abc' THEN 'abcd' ELSE b END FROM collate_test1 ORDER BY 2;
 a |  b   
---+------
 4 | ABC
 2 | äbc
 1 | abcd
 3 | bbc
(4 rows)

SELECT a, CASE b WHEN 'abc' THEN 'abcd' ELSE b END FROM collate_test2 ORDER BY 2;
 a |  b   
---+------
 4 | ABC
 1 | abcd
 3 | bbc
 2 | äbc
(4 rows)

SELECT a, CASE b WHEN 'abc' THEN 'abcd' ELSE b END FROM collate_test3 ORDER BY 2;
 a |  b   
---+------
 4 | ABC
 1 | abcd
 3 | bbc
 2 | äbc
(4 rows)

CREATE DOMAIN testdomain AS text;
SELECT a, b::testdomain FROM collate_test1 ORDER BY 2;
 a |  b  
---+-----
 1 | abc
 4 | ABC
 2 | äbc
 3 | bbc
(4 rows)

SELECT a, b::testdomain FROM collate_test2 ORDER BY 2;
 a |  b  
---+-----
 1 | abc
 4 | ABC
 3 | bbc
 2 | äbc
(4 rows)

SELECT a, b::testdomain FROM collate_test3 ORDER BY 2;
 a |  b  
---+-----
 4 | ABC
 1 | abc
 3 | bbc
 2 | äbc
(4 rows)

SELECT a, b::testdomain_sv FROM collate_test3 ORDER BY 2;
 a |  b  
---+-----
 1 | abc
 4 | ABC
 3 | bbc
 2 | äbc
(4 rows)

SELECT a, lower(x::testdomain), lower(y::testdomain) FROM collate_test10;
 a | lower | lower 
---+-------+-------
 1 | hij   | hij
 2 | hij   | hıj
(2 rows)

SELECT min(b), max(b) FROM collate_test1;
 min | max 
-----+-----
 abc | bbc
(1 row)

SELECT min(b), max(b) FROM collate_test2;
 min | max 
-----+-----
 abc | äbc
(1 row)

SELECT min(b), max(b) FROM collate_test3;
 min | max 
-----+-----
 ABC | äbc
(1 row)

SELECT array_agg(b ORDER BY b) FROM collate_test1;
     array_agg     
-------------------
 {abc,ABC,äbc,bbc}
(1 row)

SELECT array_agg(b ORDER BY b) FROM collate_test2;
     array_agg     
-------------------
 {abc,ABC,bbc,äbc}
(1 row)

SELECT array_agg(b ORDER BY b) FROM collate_test3;
     array_agg     
-------------------
 {ABC,abc,bbc,äbc}
(1 row)

SELECT a, b FROM collate_test1 UNION ALL SELECT a, b FROM collate_test1 ORDER BY 2;
 a |  b  
---+-----
 1 | abc
 1 | abc
 4 | ABC
 4 | ABC
 2 | äbc
 2 | äbc
 3 | bbc
 3 | bbc
(8 rows)

SELECT a, b FROM collate_test2 UNION SELECT a, b FROM collate_test2 ORDER BY 2;
 a |  b  
---+-----
 1 | abc
 4 | ABC
 3 | bbc
 2 | äbc
(4 rows)

SELECT a, b FROM collate_test3 WHERE a < 4 INTERSECT SELECT a, b FROM collate_test3 WHERE a > 1 ORDER BY 2;
 a |  b  
---+-----
 3 | bbc
 2 | äbc
(2 rows)

SELECT a, b FROM collate_test3 EXCEPT SELECT a, b FROM collate_test3 WHERE a < 2 ORDER BY 2;
 a |  b  
---+-----
 4 | ABC
 3 | bbc
 2 | äbc
(3 rows)

SELECT a, b FROM collate_test1 UNION ALL SELECT a, b FROM collate_test3 ORDER BY 2; -- fail
ERROR:  no collation was derived for the sort expression
LINE 1: ...e_test1 UNION ALL SELECT a, b FROM collate_test3 ORDER BY 2;
                                                                     ^
HINT:  Use the COLLATE clause to set the collation explicitly.
SELECT a, b FROM collate_test1 UNION ALL SELECT a, b FROM collate_test3; -- ok
 a |  b  
---+-----
 1 | abc
 2 | äbc
 3 | bbc
 4 | ABC
 1 | abc
 2 | äbc
 3 | bbc
 4 | ABC
(8 rows)

SELECT a, b FROM collate_test1 UNION SELECT a, b FROM collate_test3 ORDER BY 2; -- fail
ERROR:  collation mismatch between implicit collations "en_US.utf8" and "C"
LINE 1: SELECT a, b FROM collate_test1 UNION SELECT a, b FROM collat...
                                                       ^
HINT:  You can override the collation by applying the COLLATE clause to one or both expressions.
SELECT a, b COLLATE "C" FROM collate_test1 UNION SELECT a, b FROM collate_test3 ORDER BY 2; -- ok
 a |  b  
---+-----
 4 | ABC
 1 | abc
 3 | bbc
 2 | äbc
(4 rows)

SELECT a, b FROM collate_test1 INTERSECT SELECT a, b FROM collate_test3 ORDER BY 2; -- fail
ERROR:  collation mismatch between implicit collations "en_US.utf8" and "C"
LINE 1: ...ELECT a, b FROM collate_test1 INTERSECT SELECT a, b FROM col...
                                                             ^
HINT:  You can override the collation by applying the COLLATE clause to one or both expressions.
SELECT a, b FROM collate_test1 EXCEPT SELECT a, b FROM collate_test3 ORDER BY 2; -- fail
ERROR:  collation mismatch between implicit collations "en_US.utf8" and "C"
LINE 1: SELECT a, b FROM collate_test1 EXCEPT SELECT a, b FROM colla...
                                                        ^
HINT:  You can override the collation by applying the COLLATE clause to one or both expressions.
CREATE TABLE test_u AS SELECT a, b FROM collate_test1 UNION ALL SELECT a, b FROM collate_test3; -- fail
ERROR:  no collation was derived for column "b" with collatable type text
HINT:  Use the COLLATE clause to set the collation explicitly.
-- collation mismatch between recursive and non-recursive term
WITH RECURSIVE foo(x) AS
   (SELECT x FROM (VALUES('a' COLLATE "en_US"),('b')) t(x)
   UNION ALL
   SELECT (x || 'c') COLLATE "de_DE" FROM foo WHERE length(x) < 10)
SELECT * FROM foo;
ERROR:  recursive query "foo" column 1 has collation "en_US" in non-recursive term but collation "de_DE" overall
LINE 2:    (SELECT x FROM (VALUES('a' COLLATE "en_US"),('b')) t(x)
                   ^
HINT:  Use the COLLATE clause to set the collation of the non-recursive term.
-- casting
SELECT CAST('42' AS text COLLATE "C");
ERROR:  syntax error at or near "COLLATE"
LINE 1: SELECT CAST('42' AS text COLLATE "C");
                                 ^
SELECT a, CAST(b AS varchar) FROM collate_test1 ORDER BY 2;
 a |  b  
---+-----
 1 | abc
 4 | ABC
 2 | äbc
 3 | bbc
(4 rows)

SELECT a, CAST(b AS varchar) FROM collate_test2 ORDER BY 2;
 a |  b  
---+-----
 1 | abc
 4 | ABC
 3 | bbc
 2 | äbc
(4 rows)

SELECT a, CAST(b AS varchar) FROM collate_test3 ORDER BY 2;
 a |  b  
---+-----
 4 | ABC
 1 | abc
 3 | bbc
 2 | äbc
(4 rows)

-- polymorphism
SELECT * FROM unnest((SELECT array_agg(b ORDER BY b) FROM collate_test1)) ORDER BY 1;
 unnest 
--------
 abc
 ABC
 äbc
 bbc
(4 rows)

SELECT * FROM unnest((SELECT array_agg(b ORDER BY b) FROM collate_test2)) ORDER BY 1;
 unnest 
--------
 abc
 ABC
 bbc
 äbc
(4 rows)

SELECT * FROM unnest((SELECT array_agg(b ORDER BY b) FROM collate_test3)) ORDER BY 1;
 unnest 
--------
 ABC
 abc
 bbc
 äbc
(4 rows)

CREATE FUNCTION dup (f1 anyelement, f2 out anyelement, f3 out anyarray)
    AS 'select $1, array[$1,$1]' LANGUAGE sql;
SELECT a, (dup(b)).* FROM collate_test1 ORDER BY 2;
 a | f2  |    f3     
---+-----+-----------
 1 | abc | {abc,abc}
 4 | ABC | {ABC,ABC}
 2 | äbc | {äbc,äbc}
 3 | bbc | {bbc,bbc}
(4 rows)

SELECT a, (dup(b)).* FROM collate_test2 ORDER BY 2;
 a | f2  |    f3     
---+-----+-----------
 1 | abc | {abc,abc}
 4 | ABC | {ABC,ABC}
 3 | bbc | {bbc,bbc}
 2 | äbc | {äbc,äbc}
(4 rows)

SELECT a, (dup(b)).* FROM collate_test3 ORDER BY 2;
 a | f2  |    f3     
---+-----+-----------
 4 | ABC | {ABC,ABC}
 1 | abc | {abc,abc}
 3 | bbc | {bbc,bbc}
 2 | äbc | {äbc,äbc}
(4 rows)

-- indexes
CREATE INDEX collate_test1_idx1 ON collate_test1 (b);
CREATE INDEX collate_test1_idx2 ON collate_test1 (b COLLATE "C");
CREATE INDEX collate_test1_idx3 ON collate_test1 ((b COLLATE "C")); -- this is different grammatically
CREATE INDEX collate_test1_idx4 ON collate_test1 (a COLLATE "C"); -- fail
ERROR:  collations are not supported by type integer
CREATE INDEX collate_test1_idx5 ON collate_test1 ((a COLLATE "C")); -- fail
ERROR:  collations are not supported by type integer
LINE 1: ...ATE INDEX collate_test1_idx5 ON collate_test1 ((a COLLATE "C...
                                                             ^
SELECT relname, pg_get_indexdef(oid) FROM pg_class WHERE relname LIKE 'collate_test%_idx%';
      relname       |                                       pg_get_indexdef                                        
--------------------+----------------------------------------------------------------------------------------------
 collate_test1_idx1 | CREATE INDEX collate_test1_idx1 ON collate_test1 USING btree (b)
 collate_test1_idx2 | CREATE INDEX collate_test1_idx2 ON collate_test1 USING btree (b COLLATE "C")
 collate_test1_idx3 | CREATE INDEX collate_test1_idx3 ON collate_test1 USING btree (((b COLLATE "C")) COLLATE "C")
(3 rows)

-- schema manipulation commands
CREATE ROLE regress_test_role;
CREATE SCHEMA test_schema;
CREATE COLLATION test0 (locale = 'en_US.utf8');
CREATE COLLATION test0 (locale = 'en_US.utf8'); -- fail
ERROR:  collation "test0" for encoding "UTF8" already exists
CREATE COLLATION test1 (lc_collate = 'en_US.utf8', lc_ctype = 'de_DE.utf8');
CREATE COLLATION test2 (locale = 'en_US'); -- fail
ERROR:  encoding UTF8 does not match locale en_US
DETAIL:  The chosen LC_CTYPE setting requires encoding LATIN1.
CREATE COLLATION test3 (lc_collate = 'en_US.utf8'); -- fail
ERROR:  parameter "lc_ctype" must be specified
CREATE COLLATION testx (locale = 'nonsense'); -- fail
ERROR:  could not create locale "nonsense": No such file or directory
CREATE COLLATION test4 FROM nonsense;
ERROR:  collation "nonsense" for encoding "UTF8" does not exist
CREATE COLLATION test5 FROM test0;
SELECT collname, collencoding, collcollate, collctype FROM pg_collation WHERE collname LIKE 'test%' ORDER BY 1;
 collname | collencoding | collcollate | collctype  
----------+--------------+-------------+------------
 test0    |            6 | en_US.utf8  | en_US.utf8
 test1    |            6 | en_US.utf8  | de_DE.utf8
 test5    |            6 | en_US.utf8  | en_US.utf8
(3 rows)

ALTER COLLATION test1 RENAME TO test11;
ALTER COLLATION test0 RENAME TO test11; -- fail
ERROR:  collation "test11" for encoding "UTF8" already exists in schema "public"
ALTER COLLATION test1 RENAME TO test22; -- fail
ERROR:  collation "test1" for encoding "UTF8" does not exist
ALTER COLLATION test11 OWNER TO regress_test_role;
ALTER COLLATION test11 OWNER TO nonsense;
ERROR:  role "nonsense" does not exist
ALTER COLLATION test11 SET SCHEMA test_schema;
COMMENT ON COLLATION test0 IS 'US English';
SELECT collname, nspname, obj_description(pg_collation.oid, 'pg_collation')
    FROM pg_collation JOIN pg_namespace ON (collnamespace = pg_namespace.oid)
    WHERE collname LIKE 'test%'
    ORDER BY 1;
 collname |   nspname   | obj_description 
----------+-------------+-----------------
 test0    | public      | US English
 test11   | test_schema | 
 test5    | public      | 
(3 rows)

DROP COLLATION test0, test_schema.test11, test5;
DROP COLLATION test0; -- fail
ERROR:  collation "test0" for encoding "UTF8" does not exist
DROP COLLATION IF EXISTS test0;
NOTICE:  collation "test0" does not exist, skipping
SELECT collname FROM pg_collation WHERE collname LIKE 'test%';
 collname 
----------
(0 rows)

DROP SCHEMA test_schema;
DROP ROLE regress_test_role;
-- dependencies
CREATE COLLATION test0 (locale = 'en_US.utf8');
CREATE TABLE collate_dep_test1 (a int, b text COLLATE test0);
CREATE DOMAIN collate_dep_dom1 AS text COLLATE test0;
CREATE TYPE collate_dep_test2 AS (x int, y text COLLATE test0);
CREATE VIEW collate_dep_test3 AS SELECT text 'foo' COLLATE test0 AS foo;
CREATE TABLE collate_dep_test4t (a int, b text);
CREATE INDEX collate_dep_test4i ON collate_dep_test4t (b COLLATE test0);
DROP COLLATION test0 RESTRICT; -- fail
ERROR:  cannot drop collation test0 because other objects depend on it
DETAIL:  table collate_dep_test1 column b depends on collation test0
type collate_dep_dom1 depends on collation test0
composite type collate_dep_test2 column y depends on collation test0
view collate_dep_test3 depends on collation test0
index collate_dep_test4i depends on collation test0
HINT:  Use DROP ... CASCADE to drop the dependent objects too.
DROP COLLATION test0 CASCADE;
NOTICE:  drop cascades to 5 other objects
DETAIL:  drop cascades to table collate_dep_test1 column b
drop cascades to type collate_dep_dom1
drop cascades to composite type collate_dep_test2 column y
drop cascades to view collate_dep_test3
drop cascades to index collate_dep_test4i
\d collate_dep_test1
Table "public.collate_dep_test1"
 Column |  Type   | Modifiers 
--------+---------+-----------
 a      | integer | 

\d collate_dep_test2
Composite type "public.collate_dep_test2"
 Column |  Type   
--------+---------
 x      | integer

DROP TABLE collate_dep_test1, collate_dep_test4t;
DROP TYPE collate_dep_test2;
