1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
|
/*
* This test is intended to pass on all platforms supported by Postgres.
* We can therefore only assume that the default, C, and POSIX collations
* are available --- and since the regression tests are often run in a
* C-locale database, these may well all have the same behavior. But
* fortunately, the system doesn't know that and will treat them as
* incompatible collations. It is therefore at least possible to test
* parser behaviors such as collation conflict resolution. This test will,
* however, be more revealing when run in a database with non-C locale,
* since any departure from C sorting behavior will show as a failure.
*/
CREATE SCHEMA collate_tests;
SET search_path = collate_tests;
CREATE TABLE collate_test1 (
a int,
b text COLLATE "C" NOT NULL
);
\d collate_test1
Table "collate_tests.collate_test1"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
a | integer | | |
b | text | C | not null |
CREATE TABLE collate_test_fail (
a int COLLATE "C",
b text
);
ERROR: collations are not supported by type integer
LINE 2: a int COLLATE "C",
^
CREATE TABLE collate_test_like (
LIKE collate_test1
);
\d collate_test_like
Table "collate_tests.collate_test_like"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
a | integer | | |
b | text | C | not null |
CREATE TABLE collate_test2 (
a int,
b text COLLATE "POSIX"
);
INSERT INTO collate_test1 VALUES (1, 'abc'), (2, 'Abc'), (3, 'bbc'), (4, 'ABD');
INSERT INTO collate_test2 SELECT * FROM collate_test1;
SELECT * FROM collate_test1 WHERE b COLLATE "C" >= 'abc';
a | b
---+-----
1 | abc
3 | bbc
(2 rows)
SELECT * FROM collate_test1 WHERE b >= 'abc' COLLATE "C";
a | b
---+-----
1 | abc
3 | bbc
(2 rows)
SELECT * FROM collate_test1 WHERE b COLLATE "C" >= 'abc' COLLATE "C";
a | b
---+-----
1 | abc
3 | bbc
(2 rows)
SELECT * FROM collate_test1 WHERE b COLLATE "C" >= 'bbc' COLLATE "POSIX"; -- fail
ERROR: collation mismatch between explicit collations "C" and "POSIX"
LINE 1: ...* FROM collate_test1 WHERE b COLLATE "C" >= 'bbc' COLLATE "P...
^
CREATE DOMAIN testdomain_p AS text COLLATE "POSIX";
CREATE DOMAIN testdomain_i AS int COLLATE "POSIX"; -- fail
ERROR: collations are not supported by type integer
CREATE TABLE collate_test4 (
a int,
b testdomain_p
);
INSERT INTO collate_test4 SELECT * FROM collate_test1;
SELECT a, b FROM collate_test4 ORDER BY b;
a | b
---+-----
4 | ABD
2 | Abc
1 | abc
3 | bbc
(4 rows)
CREATE TABLE collate_test5 (
a int,
b testdomain_p COLLATE "C"
);
INSERT INTO collate_test5 SELECT * FROM collate_test1;
SELECT a, b FROM collate_test5 ORDER BY b;
a | b
---+-----
4 | ABD
2 | Abc
1 | abc
3 | bbc
(4 rows)
SELECT a, b FROM collate_test1 ORDER BY b;
a | b
---+-----
4 | ABD
2 | Abc
1 | abc
3 | bbc
(4 rows)
SELECT a, b FROM collate_test2 ORDER BY b;
a | b
---+-----
4 | ABD
2 | Abc
1 | abc
3 | bbc
(4 rows)
SELECT a, b FROM collate_test1 ORDER BY b COLLATE "C";
a | b
---+-----
4 | ABD
2 | Abc
1 | abc
3 | bbc
(4 rows)
-- star expansion
SELECT * FROM collate_test1 ORDER BY b;
a | b
---+-----
4 | ABD
2 | Abc
1 | abc
3 | bbc
(4 rows)
SELECT * FROM collate_test2 ORDER BY b;
a | b
---+-----
4 | ABD
2 | Abc
1 | abc
3 | bbc
(4 rows)
-- constant expression folding
SELECT 'bbc' COLLATE "C" > 'Abc' COLLATE "C" AS "true";
true
------
t
(1 row)
SELECT 'bbc' COLLATE "POSIX" < 'Abc' COLLATE "POSIX" AS "false";
false
-------
f
(1 row)
-- upper/lower
CREATE TABLE collate_test10 (
a int,
x text COLLATE "C",
y text COLLATE "POSIX"
);
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 | HIJ | Hij | Hij
2 | hij | hij | HIJ | HIJ | Hij | Hij
(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
---+-----+-----
1 | hij | hij
2 | HIJ | HIJ
(2 rows)
-- 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 "POSIX") 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 "POSIX")) AS lower+
| FROM collate_test10;
(3 rows)
-- collation propagation in various expression types
SELECT a, coalesce(b, 'foo') FROM collate_test1 ORDER BY 2;
a | coalesce
---+----------
4 | ABD
2 | Abc
1 | abc
3 | bbc
(4 rows)
SELECT a, coalesce(b, 'foo') FROM collate_test2 ORDER BY 2;
a | coalesce
---+----------
4 | ABD
2 | Abc
1 | abc
3 | bbc
(4 rows)
SELECT a, lower(coalesce(x, 'foo')), lower(coalesce(y, 'foo')) FROM collate_test10;
a | lower | lower
---+-------+-------
1 | hij | hij
2 | hij | hij
(2 rows)
SELECT a, b, greatest(b, 'CCC') FROM collate_test1 ORDER BY 3;
a | b | greatest
---+-----+----------
2 | Abc | CCC
4 | ABD | CCC
1 | abc | abc
3 | bbc | bbc
(4 rows)
SELECT a, b, greatest(b, 'CCC') FROM collate_test2 ORDER BY 3;
a | b | greatest
---+-----+----------
2 | Abc | CCC
4 | ABD | CCC
1 | abc | abc
3 | bbc | bbc
(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 | foo | foo
(2 rows)
SELECT a, nullif(b, 'abc') FROM collate_test1 ORDER BY 2;
a | nullif
---+--------
4 | ABD
2 | Abc
3 | bbc
1 |
(4 rows)
SELECT a, nullif(b, 'abc') FROM collate_test2 ORDER BY 2;
a | nullif
---+--------
4 | ABD
2 | Abc
3 | bbc
1 |
(4 rows)
SELECT a, lower(nullif(x, 'foo')), lower(nullif(y, 'foo')) FROM collate_test10;
a | lower | lower
---+-------+-------
1 | hij | hij
2 | hij | hij
(2 rows)
SELECT a, CASE b WHEN 'abc' THEN 'abcd' ELSE b END FROM collate_test1 ORDER BY 2;
a | b
---+------
4 | ABD
2 | Abc
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 | ABD
2 | Abc
1 | abcd
3 | bbc
(4 rows)
CREATE DOMAIN testdomain AS text;
SELECT a, b::testdomain FROM collate_test1 ORDER BY 2;
a | b
---+-----
4 | ABD
2 | Abc
1 | abc
3 | bbc
(4 rows)
SELECT a, b::testdomain FROM collate_test2 ORDER BY 2;
a | b
---+-----
4 | ABD
2 | Abc
1 | abc
3 | bbc
(4 rows)
SELECT a, b::testdomain_p FROM collate_test2 ORDER BY 2;
a | b
---+-----
4 | ABD
2 | Abc
1 | abc
3 | bbc
(4 rows)
SELECT a, lower(x::testdomain), lower(y::testdomain) FROM collate_test10;
a | lower | lower
---+-------+-------
1 | hij | hij
2 | hij | hij
(2 rows)
SELECT min(b), max(b) FROM collate_test1;
min | max
-----+-----
ABD | bbc
(1 row)
SELECT min(b), max(b) FROM collate_test2;
min | max
-----+-----
ABD | bbc
(1 row)
SELECT array_agg(b ORDER BY b) FROM collate_test1;
array_agg
-------------------
{ABD,Abc,abc,bbc}
(1 row)
SELECT array_agg(b ORDER BY b) FROM collate_test2;
array_agg
-------------------
{ABD,Abc,abc,bbc}
(1 row)
-- In aggregates, ORDER BY expressions don't affect aggregate's collation
SELECT string_agg(x COLLATE "C", y COLLATE "POSIX") FROM collate_test10; -- fail
ERROR: collation mismatch between explicit collations "C" and "POSIX"
LINE 1: SELECT string_agg(x COLLATE "C", y COLLATE "POSIX") FROM col...
^
SELECT array_agg(x COLLATE "C" ORDER BY y COLLATE "POSIX") FROM collate_test10;
array_agg
-----------
{HIJ,hij}
(1 row)
SELECT array_agg(a ORDER BY x COLLATE "C", y COLLATE "POSIX") FROM collate_test10;
array_agg
-----------
{2,1}
(1 row)
SELECT array_agg(a ORDER BY x||y) FROM collate_test10; -- fail
ERROR: collation mismatch between implicit collations "C" and "POSIX"
LINE 1: SELECT array_agg(a ORDER BY x||y) FROM collate_test10;
^
HINT: You can choose the collation by applying the COLLATE clause to one or both expressions.
SELECT a, b FROM collate_test1 UNION ALL SELECT a, b FROM collate_test1 ORDER BY 2;
a | b
---+-----
4 | ABD
4 | ABD
2 | Abc
2 | Abc
1 | abc
1 | abc
3 | bbc
3 | bbc
(8 rows)
SELECT a, b FROM collate_test2 UNION SELECT a, b FROM collate_test2 ORDER BY 2;
a | b
---+-----
4 | ABD
2 | Abc
1 | abc
3 | bbc
(4 rows)
SELECT a, b FROM collate_test2 WHERE a < 4 INTERSECT SELECT a, b FROM collate_test2 WHERE a > 1 ORDER BY 2;
a | b
---+-----
2 | Abc
3 | bbc
(2 rows)
SELECT a, b FROM collate_test2 EXCEPT SELECT a, b FROM collate_test2 WHERE a < 2 ORDER BY 2;
a | b
---+-----
4 | ABD
2 | Abc
3 | bbc
(3 rows)
SELECT a, b FROM collate_test1 UNION ALL SELECT a, b FROM collate_test2 ORDER BY 2; -- fail
ERROR: could not determine which collation to use for string comparison
HINT: Use the COLLATE clause to set the collation explicitly.
SELECT a, b FROM collate_test1 UNION ALL SELECT a, b FROM collate_test2; -- ok
a | b
---+-----
1 | abc
2 | Abc
3 | bbc
4 | ABD
1 | abc
2 | Abc
3 | bbc
4 | ABD
(8 rows)
SELECT a, b FROM collate_test1 UNION SELECT a, b FROM collate_test2 ORDER BY 2; -- fail
ERROR: collation mismatch between implicit collations "C" and "POSIX"
LINE 1: SELECT a, b FROM collate_test1 UNION SELECT a, b FROM collat...
^
HINT: You can choose 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_test2 ORDER BY 2; -- ok
a | b
---+-----
4 | ABD
2 | Abc
1 | abc
3 | bbc
(4 rows)
SELECT a, b FROM collate_test1 INTERSECT SELECT a, b FROM collate_test2 ORDER BY 2; -- fail
ERROR: collation mismatch between implicit collations "C" and "POSIX"
LINE 1: ...ELECT a, b FROM collate_test1 INTERSECT SELECT a, b FROM col...
^
HINT: You can choose the collation by applying the COLLATE clause to one or both expressions.
SELECT a, b FROM collate_test1 EXCEPT SELECT a, b FROM collate_test2 ORDER BY 2; -- fail
ERROR: collation mismatch between implicit collations "C" and "POSIX"
LINE 1: SELECT a, b FROM collate_test1 EXCEPT SELECT a, b FROM colla...
^
HINT: You can choose 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_test2; -- fail
ERROR: no collation was derived for column "b" with collatable type text
HINT: Use the COLLATE clause to set the collation explicitly.
-- ideally this would be a parse-time error, but for now it must be run-time:
select x < y from collate_test10; -- fail
ERROR: could not determine which collation to use for string comparison
HINT: Use the COLLATE clause to set the collation explicitly.
select x || y from collate_test10; -- ok, because || is not collation aware
?column?
----------
hijhij
HIJHIJ
(2 rows)
select x, y from collate_test10 order by x || y; -- not so ok
ERROR: collation mismatch between implicit collations "C" and "POSIX"
LINE 1: select x, y from collate_test10 order by x || y;
^
HINT: You can choose the collation by applying the COLLATE clause to one or both expressions.
-- collation mismatch between recursive and non-recursive term
WITH RECURSIVE foo(x) AS
(SELECT x FROM (VALUES('a' COLLATE "C"),('b')) t(x)
UNION ALL
SELECT (x || 'c') COLLATE "POSIX" FROM foo WHERE length(x) < 10)
SELECT * FROM foo;
ERROR: recursive query "foo" column 1 has collation "C" in non-recursive term but collation "POSIX" overall
LINE 2: (SELECT x FROM (VALUES('a' COLLATE "C"),('b')) t(x)
^
HINT: Use the COLLATE clause to set the collation of the non-recursive term.
SELECT a, b, a < b as lt FROM
(VALUES ('a', 'B'), ('A', 'b' COLLATE "C")) v(a,b);
a | b | lt
---+---+----
a | B | f
A | b | t
(2 rows)
-- collation mismatch in subselects
SELECT * FROM collate_test10 WHERE (x, y) NOT IN (SELECT y, x FROM collate_test10);
ERROR: could not determine which collation to use for string hashing
HINT: Use the COLLATE clause to set the collation explicitly.
-- now it works with overrides
SELECT * FROM collate_test10 WHERE (x COLLATE "POSIX", y COLLATE "C") NOT IN (SELECT y, x FROM collate_test10);
a | x | y
---+---+---
(0 rows)
SELECT * FROM collate_test10 WHERE (x, y) NOT IN (SELECT y COLLATE "C", x COLLATE "POSIX" FROM collate_test10);
a | x | y
---+---+---
(0 rows)
-- 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
---+-----
4 | ABD
2 | Abc
1 | abc
3 | bbc
(4 rows)
SELECT a, CAST(b AS varchar) FROM collate_test2 ORDER BY 2;
a | b
---+-----
4 | ABD
2 | Abc
1 | abc
3 | bbc
(4 rows)
-- result of a SQL function
CREATE FUNCTION vc (text) RETURNS text LANGUAGE sql
AS 'select $1::varchar';
SELECT a, b FROM collate_test1 ORDER BY a, vc(b);
a | b
---+-----
1 | abc
2 | Abc
3 | bbc
4 | ABD
(4 rows)
-- polymorphism
SELECT * FROM unnest((SELECT array_agg(b ORDER BY b) FROM collate_test1)) ORDER BY 1;
unnest
--------
ABD
Abc
abc
bbc
(4 rows)
SELECT * FROM unnest((SELECT array_agg(b ORDER BY b) FROM collate_test2)) ORDER BY 1;
unnest
--------
ABD
Abc
abc
bbc
(4 rows)
CREATE FUNCTION dup (anyelement) RETURNS anyelement
AS 'select $1' LANGUAGE sql;
SELECT a, dup(b) FROM collate_test1 ORDER BY 2;
a | dup
---+-----
4 | ABD
2 | Abc
1 | abc
3 | bbc
(4 rows)
SELECT a, dup(b) FROM collate_test2 ORDER BY 2;
a | dup
---+-----
4 | ABD
2 | Abc
1 | abc
3 | bbc
(4 rows)
-- indexes
CREATE INDEX collate_test1_idx1 ON collate_test1 (b);
CREATE INDEX collate_test1_idx2 ON collate_test1 (b COLLATE "POSIX");
CREATE INDEX collate_test1_idx3 ON collate_test1 ((b COLLATE "POSIX")); -- this is different grammatically
CREATE INDEX collate_test1_idx4 ON collate_test1 (((b||'foo') COLLATE "POSIX"));
CREATE INDEX collate_test1_idx5 ON collate_test1 (a COLLATE "POSIX"); -- fail
ERROR: collations are not supported by type integer
CREATE INDEX collate_test1_idx6 ON collate_test1 ((a COLLATE "POSIX")); -- fail
ERROR: collations are not supported by type integer
LINE 1: ...ATE INDEX collate_test1_idx6 ON collate_test1 ((a COLLATE "P...
^
SELECT relname, pg_get_indexdef(oid) FROM pg_class WHERE relname LIKE 'collate_test%_idx%' ORDER BY 1;
relname | pg_get_indexdef
--------------------+-------------------------------------------------------------------------------------------------------------------
collate_test1_idx1 | CREATE INDEX collate_test1_idx1 ON collate_tests.collate_test1 USING btree (b)
collate_test1_idx2 | CREATE INDEX collate_test1_idx2 ON collate_tests.collate_test1 USING btree (b COLLATE "POSIX")
collate_test1_idx3 | CREATE INDEX collate_test1_idx3 ON collate_tests.collate_test1 USING btree (b COLLATE "POSIX")
collate_test1_idx4 | CREATE INDEX collate_test1_idx4 ON collate_tests.collate_test1 USING btree (((b || 'foo'::text)) COLLATE "POSIX")
(4 rows)
-- foreign keys
-- force indexes and mergejoins to be used for FK checking queries,
-- else they might not exercise collation-dependent operators
SET enable_seqscan TO 0;
SET enable_hashjoin TO 0;
SET enable_nestloop TO 0;
CREATE TABLE collate_test20 (f1 text COLLATE "C" PRIMARY KEY);
INSERT INTO collate_test20 VALUES ('foo'), ('bar');
CREATE TABLE collate_test21 (f2 text COLLATE "POSIX" REFERENCES collate_test20);
INSERT INTO collate_test21 VALUES ('foo'), ('bar');
INSERT INTO collate_test21 VALUES ('baz'); -- fail
ERROR: insert or update on table "collate_test21" violates foreign key constraint "collate_test21_f2_fkey"
DETAIL: Key (f2)=(baz) is not present in table "collate_test20".
CREATE TABLE collate_test22 (f2 text COLLATE "POSIX");
INSERT INTO collate_test22 VALUES ('foo'), ('bar'), ('baz');
ALTER TABLE collate_test22 ADD FOREIGN KEY (f2) REFERENCES collate_test20; -- fail
ERROR: insert or update on table "collate_test22" violates foreign key constraint "collate_test22_f2_fkey"
DETAIL: Key (f2)=(baz) is not present in table "collate_test20".
DELETE FROM collate_test22 WHERE f2 = 'baz';
ALTER TABLE collate_test22 ADD FOREIGN KEY (f2) REFERENCES collate_test20;
RESET enable_seqscan;
RESET enable_hashjoin;
RESET enable_nestloop;
-- EXPLAIN
EXPLAIN (COSTS OFF)
SELECT * FROM collate_test10 ORDER BY x, y;
QUERY PLAN
----------------------------------------------
Sort
Sort Key: x COLLATE "C", y COLLATE "POSIX"
-> Seq Scan on collate_test10
(3 rows)
EXPLAIN (COSTS OFF)
SELECT * FROM collate_test10 ORDER BY x DESC, y COLLATE "C" ASC NULLS FIRST;
QUERY PLAN
-----------------------------------------------------------
Sort
Sort Key: x COLLATE "C" DESC, y COLLATE "C" NULLS FIRST
-> Seq Scan on collate_test10
(3 rows)
-- CREATE/DROP COLLATION
CREATE COLLATION mycoll1 FROM "C";
CREATE COLLATION mycoll2 ( LC_COLLATE = "POSIX", LC_CTYPE = "POSIX" );
CREATE COLLATION mycoll3 FROM "default"; -- intentionally unsupported
ERROR: collation "default" cannot be copied
DROP COLLATION mycoll1;
CREATE TABLE collate_test23 (f1 text collate mycoll2);
DROP COLLATION mycoll2; -- fail
ERROR: cannot drop collation mycoll2 because other objects depend on it
DETAIL: column f1 of table collate_test23 depends on collation mycoll2
HINT: Use DROP ... CASCADE to drop the dependent objects too.
-- invalid: non-lowercase quoted identifiers
CREATE COLLATION case_coll ("Lc_Collate" = "POSIX", "Lc_Ctype" = "POSIX");
ERROR: collation attribute "Lc_Collate" not recognized
LINE 1: CREATE COLLATION case_coll ("Lc_Collate" = "POSIX", "Lc_Ctyp...
^
-- 9.1 bug with useless COLLATE in an expression subject to length coercion
CREATE TEMP TABLE vctable (f1 varchar(25));
INSERT INTO vctable VALUES ('foo' COLLATE "C");
SELECT collation for ('foo'); -- unknown type - null
pg_collation_for
------------------
(1 row)
SELECT collation for ('foo'::text);
pg_collation_for
------------------
"default"
(1 row)
SELECT collation for ((SELECT a FROM collate_test1 LIMIT 1)); -- non-collatable type - error
ERROR: collations are not supported by type integer
SELECT collation for ((SELECT b FROM collate_test1 LIMIT 1));
pg_collation_for
------------------
"C"
(1 row)
-- old bug with not dropping COLLATE when coercing to non-collatable type
CREATE VIEW collate_on_int AS
SELECT c1+1 AS c1p FROM
(SELECT ('4' COLLATE "C")::INT AS c1) ss;
\d+ collate_on_int
View "collate_tests.collate_on_int"
Column | Type | Collation | Nullable | Default | Storage | Description
--------+---------+-----------+----------+---------+---------+-------------
c1p | integer | | | | plain |
View definition:
SELECT ss.c1 + 1 AS c1p
FROM ( SELECT 4 AS c1) ss;
--
-- Clean up. Many of these table names will be re-used if the user is
-- trying to run any platform-specific collation tests later, so we
-- must get rid of them.
--
DROP SCHEMA collate_tests CASCADE;
NOTICE: drop cascades to 19 other objects
DETAIL: drop cascades to table collate_test1
drop cascades to table collate_test_like
drop cascades to table collate_test2
drop cascades to type testdomain_p
drop cascades to table collate_test4
drop cascades to table collate_test5
drop cascades to table collate_test10
drop cascades to view collview1
drop cascades to view collview2
drop cascades to view collview3
drop cascades to type testdomain
drop cascades to function vc(text)
drop cascades to function dup(anyelement)
drop cascades to table collate_test20
drop cascades to table collate_test21
drop cascades to table collate_test22
drop cascades to collation mycoll2
drop cascades to table collate_test23
drop cascades to view collate_on_int
|